Push Operation in Stacks

by Jasleen Chhabra | Updated on 24 August 2024

The push operation is one of the fundamental actions you can perform on a stack. It adds a new element to the top of the stack, following the Last In, First Out (LIFO) principle. Whether implemented using an array or a linked list, the push operation is essential for maintaining the stack’s order and structure.

What is the Push Operation?

In a stack, the push operation is responsible for adding a new element to the top of the stack. This operation ensures that the most recently added element is the first one to be removed when a pop operation is performed. The push operation must handle both normal scenarios and edge cases, such as stack overflow in fixed-size implementations.

Steps of the Push Operation

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

  1. Check for Overflow: Ensure that there is enough space to add a new element. In fixed-size stacks, this means checking if the stack is already full.
  2. Increment the Top Index: Move the top index to the next position to make space for the new element.
  3. Add the New Element: Place the new element at the position indicated by the top index.

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

Algorithm for Push Operation

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

  1. Start
  2. Check for Stack Overflow
    • If the top index is equal to the maximum size minus one, print "Stack Overflow" and exit the operation.
  3. Increment the Top Index
    • top = top + 1
  4. Add the New Element
    • stack[top] = newElement
  5. End

Pseudocode for Push Operation

Algorithm Push(stack, top, maxSize, newElement):
    if top >= maxSize - 1:
        print "Stack Overflow"
        return
    top = top + 1
    stack[top] = newElement

 

C++ Implementation of Push Operation

Here’s how you can implement the push 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);
    bool isFull();
    void printStack();
};

bool Stack::isFull() {
    return (top >= (MAX - 1));
}

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

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();
    
    return 0;
}

 

Handling Stack Overflow

In the example above, we’ve included a check for stack overflow. If the stack is full, the push operation will not add the new element and will print a "Stack Overflow" message instead. This is crucial for preventing data corruption and ensuring the stack operates correctly.

Conclusion

The push operation is vital for adding new elements to a stack, maintaining its LIFO structure. By understanding and implementing the push 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 overflow, ensures robust and reliable stack operations, making your programs more efficient and error-free.


FAQ

Any Questions?
Look Here.

Related Articles

Introduction to Stacks

Pop Operation in Stacks