Stack in C++

What is Stack?

Stack is a data structure that follows LIFO(last in first out) principle, Stack can be visualized as a pile of books, when we pile a stack of books in a box we only have access to the topmost book if we have to remove a book or pick a book we will only be able to access the topmost book.

Why is Stack useful?

Stack has quite a few advantages as a data structure some of them are the following:-

  • Arithmetic operations: Stack is very useful in performing arithmetic operations such as in evaluating mathematical expressions and postfix operations.

  • LIFO operation: Stack is especially helpful when we need to process items in reverse or while using a backtracking algorithm or undoing any task.

  • Simplicity: Stack is a fairly simple data structure with few operations like push, pop, size, and top which makes it easy to use.

  • Memory management: Stacks use a contiguous block of memory, which allows for efficient memory management. The allocation and deallocation of memory for stack elements follow a simple and predictable pattern, resulting in minimal overhead.

  • Recursion: Stacks are essential for handling recursive function calls. Recursive algorithms rely on the stack to keep track of function calls and their associated data. As each recursive call is made, a new stack frame is created, and when the recursion reaches the base case, the stack frames are gradually popped, allowing the algorithm to return results in the correct order.

Syntax of Stack functions:

  1. Push: push method is used to add elements in the stack, elements are also added in the LIFO manner.

    Syntax: stack name.push(item);

  2. Pop: pop method removes the element from the top of the stack if present.

    Syntax: stackName.pop();

  3. Top: This method returns the top element from the stack if present.

    Syntax: stackName.top();

  4. Empty: This method tells us whether the stack is empty or not.

    Syntax: stackEmpty();

  5. Size: This method is used to find the number of elements present in the stack.

    Syntax: stackName.size();

Example:

#include "iostream"
#include "stack"

using namespace std;

int main() {

    stack<char>st;

    // PUSH
    cout << "Pushing a and b into the stack" << endl;
    st.push('a');
    st.push('b');

    // TOP
    cout << "The top element is: " << st.top() << endl;

    // POP
    cout << "Popping an element" << endl;
    st.pop();
    cout << "The top element is: " << st.top() << endl;

    // EMPTY
    cout << "Checking if the stack is empty: ";
    cout << st.empty() << endl;

    // SIZE
    cout << "Size of the stack: " << st.size() << endl;

    cout << "Popping another element" << endl;
    st.pop();

    cout << "Size of the stack: " << st.size() << endl;
    cout << "Checking if the stack is empty: ";
    cout << st.empty() << endl;
    return 0;
}

Output:

Pushing a and b into the stack
The top element is: b
Popping an element
The top element is: a
Checking if the stack is empty: 0
Size of the stack: 1
Popping another element
Size of the stack: 0
Checking if the stack is empty: 1