0
Explore
0

Push and Pop Operation in Stack: Algorithm and Program

Updated on October 4, 2025

A stack is a Linear Abstract Data Type (ADT) that follows the LIFO(Last in first out) property.

There are two operation which can be performed on stack

1. PUSH

2. POP

1. PUSH operation of Stack

PUSH operation of the stack is used to add an item to a stack at the top. We can perform Push operation only at the top of the stack. However, before inserting an item in the stack we must check stack should have some empty space.

Push operation can be performed in the below steps

Step 1 Checks stack has some space or stack is full.

Step 2 − If the stack has no space then display “overflow” and exit.

Step 3 − If the stack has space then increase top by 1 to point next empty space.

Step 4 − Adds item to the newly stack location, where top is pointing.

Step 5 – PUSH operation performed successfully.

PUSH Operation Algorithm of Stack

if TOP = MAX-1
       return "Overflow"
 endif
    top = top + 1
    stack[top] = item
 end

Code Snippet of PUSH Operation

public void push(int x) {
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
        } else {
            a[++top] = x;
            System.out.println("Element   pushed into stack = " + x);
        }
    }

Java Program to perform Push Operation

public class Main {
    static final int MAX = 1000; // Maximum size of Stack
    int top;
    int[] a = new int[MAX]; // Stack
    
    // Constructor
    Main() {
        top = -1;
    }
    
    // Push method
    public void push(int x) {
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
        } else {
            a[++top] = x;
            System.out.println("Element pushed into stack = " + x);
        }
    }
    
    // Main method to test
    public static void main(String[] args) {
        Main stack = new Main();
        
        // Push elements
        stack.push(50);
        stack.push(60);
        stack.push(70);
        stack.push(80);
        // Try pushing more elements if needed
    }
}

Output

Element pushed into stack = 50
Element pushed into stack = 60
Element pushed into stack = 70
Element pushed into stack = 80

Explanation of code

In the above program, the class Main defines a stack with a maximum size of 1000 elements, represented by the constant MAX. The variable top keeps track of the index of the top element in the stack, and it is initialized to -1 in the constructor, which means the stack is initially empty.

The push(int x) method is used to add elements into the stack. Before inserting, it checks if the stack is already full by verifying whether top is greater than or equal to MAX - 1. If it is full, it displays a message “Stack Overflow”, indicating that no more elements can be added. Otherwise, it increases the value of top by one and inserts the new element at that position, then prints a message showing which element was pushed.

In the main() method, an object of the Main class named stack is created. Then, several elements 50, 60, 70, and 80—are pushed into the stack using the push() method. Each successful insertion displays a confirmation message.

2. POP Operation of Stack

POP operation is performed on the stack to remove items from the stack. We can perform the Pop operation only at the top of the stack. In an array implementation of pop() operation, the data element is not actually removed, instead the top is decremented to a lower position in the stack to point to the next value.

POP operation can be Performed in the below steps

Step 1 − Checks stack has some element or stack is empty.

Step 2 − If the stack has no element means it is empty then display “underflow”

Step 3 − If the stack has element some element, accesses the data element at which top is pointing.

Step 4 − Decreases the value of top by 1.

Step 5 − POP operation performed successfully.

POP Operation Algorithm of Stack

If TOP=-1
       return "Underflow"
 endif
    item=Stack[Top]
     top=top-1
     return Item
 end

Code Snippet of POP Operation

int pop(struct Stack* stack) {
    if (stack->top < 0) {
        printf("Stack Underflow\n");
        return 0;
    } else {
        int x = stack->a[stack->top--];
        return x;
    }
}

Java Program to perform Pop Operation

public class Main {
    private static final int MAX = 1000; // Maximum size of the stack
    private int top; // Top of the stack
    private int[] a = new int[MAX]; // Array to store the stack elements
    
    public Main() {
        top = -1; // Initialize top of stack
    }
    
    // Method to push an element onto the stack
    public void push(int x) {
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
        } else {
            a[++top] = x; // Increment top and add the element
            System.out.println("Pushed element in stack= "+x);
        }
    }
    
    // Method to pop an element from the top of the stack
    public int pop() {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0; // Return 0 to indicate the operation failed
        } else {
            int x = a[top--]; // Retrieve the top element and decrement top
            return x;
        }
    }
    
    public static void main(String[] args) {
        Main stack = new Main();
        
        // Push elements to the stack
        stack.push(20);
        stack.push(40);
        stack.push(60);
        stack.push(80);
        
        // Pop elements from the stack and print them
        System.out.println("Popped element from stack= "+stack.pop());
        System.out.println("Popped elements from stack= "+stack.pop());
        
    }
}

Output

Pushed element in stack= 20
Pushed element in stack= 40
Pushed element in stack= 60
Pushed element in stack= 80
Popped element from stack= 80
Popped elements from stack= 60

Explanation of code

In the above program, the class Main defines a stack with a maximum capacity of 1000 elements, stored in an integer array a. The variable top keeps track of the index of the topmost element in the stack. Initially, top is set to -1 in the constructor, which indicates that the stack is empty.

The push(int x) method is used to add an element to the top of the stack. Before inserting, it checks whether the stack is full by comparing top with MAX - 1. If the stack is full, it prints “Stack Overflow”; otherwise, it increments top and stores the new element at that position, displaying a message confirming the insertion.

The pop() method is used to remove the top element from the stack. Before removing, it checks if the stack is empty (i.e., if top < 0). If it is empty, it displays “Stack Underflow” and returns 0 to indicate that no element could be removed. If the stack is not empty, it retrieves the top element, decreases top by one, and returns the removed value.

In the main() method, an object stack of the Main class is created. Four elements 20, 40, 60, and 80 are pushed onto the stack using the push() method. Then, two elements are removed from the stack using the pop() method, and the popped elements are displayed on the screen.