Push and Pop Operation in Stack: Algorithm and Program

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

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