Pop Operation in Stacks

by Jasleen Chhabra | Updated on 24 August 2024

The pop operation is one of the core actions performed on a stack. It removes the top element, adhering to the Last In, First Out (LIFO) principle. Whether the stack is implemented using an array or a linked list, the pop operation is fundamental to maintaining the stack’s order and structure.

What is the Pop Operation?

In a stack, the pop operation removes and returns the top element. This operation ensures that the most recently added element is the first one to be removed, following the LIFO principle. The pop operation must handle both regular scenarios and edge cases, such as stack underflow when attempting to pop from an empty stack.

Steps of the Pop Operation

When a stack is implemented using an array, the pop operation involves the following steps:

  1. Check for Underflow: Ensure that the stack is not empty. If it is, the operation cannot proceed.
  2. Return the Top Element: Retrieve the element located at the current top index.
  3. Decrement the Top Index: Move the top index down to the previous element.

These steps ensure that the stack maintains its LIFO order and can efficiently handle the removal of elements.

Algorithm for Pop Operation

Here’s a step-by-step algorithm for performing the pop operation on a stack implemented using an array:

  1. Start
  2. Check for Stack Underflow
    • If the top index is less than 0, print "Stack Underflow" and exit the operation.
  3. Return the Top Element
    • element = stack[top]
  4. Decrement the Top Index
    • top = top - 1
  5. End

Pseudocode for Pop Operation

Algorithm Pop(stack, top):
    if top < 0:
        print "Stack Underflow"
        return None
    element = stack[top]
    top = top - 1
    return element

 

C++ Implementation of Pop Operation

Here’s how you can implement the pop operation in C++ using an array-based stack:

#include <iostream>
#define MAX 1000

class Stack {
    int top;
    int arr[MAX]; // Maximum size of Stack

public:
    Stack() { top = -1; }
    bool push(int x);
    int pop();
    bool isEmpty();
    void printStack();
};

bool Stack::isEmpty() {
    return (top < 0);
}

bool Stack::push(int x) {
    if (top >= (MAX - 1)) {
        std::cout << "Stack Overflow" << std::endl;
        return false;
    } else {
        arr[++top] = x;
        std::cout << x << " pushed into stack" << std::endl;
        return true;
    }
}

int Stack::pop() {
    if (isEmpty()) {
        std::cout << "Stack Underflow" << std::endl;
        return -1;
    } else {
        int x = arr[top--];
        return x;
    }
}

void Stack::printStack() {
    for (int i = 0; i <= top; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
}

int main() {
    Stack stack;
    stack.push(10);
    stack.push(20);
    stack.push(30);
    stack.printStack();
    
    std::cout << "Popped from stack: " << stack.pop() << std::endl;
    stack.printStack();
    
    return 0;
}

Handling Stack Underflow

In the example above, we’ve included a check for stack underflow. If the stack is empty, the pop operation will not proceed and will print a "Stack Underflow" message instead. This is crucial for preventing errors and ensuring the stack operates correctly.

Conclusion

The pop operation is essential for removing elements from a stack while maintaining its LIFO structure. By understanding and implementing the pop operation correctly, you can efficiently manage data in stacks, whether you're working with arrays or linked lists. Proper handling of edge cases, like stack underflow, ensures robust and reliable stack operations, making your programs more efficient and error-free.


FAQ

Any Questions?
Look Here.

Related Articles

Introduction to Stacks

Push Operation in Stacks