The stack is a fundamental data structure in computer science, commonly used for managing data in Last-In, First-Out (LIFO) order. In a stack, the last element inserted is the first one to be removed, which is analogous to a stack of plates where the last one placed on top is the first one you would pick up.
Stacks are used in various applications:
Stacks can be implemented in C++ using arrays, linked lists, or through the Standard Template Library (STL). Here’s a deeper dive into each method:
An array-based implementation uses a fixed-size array and an index that points to the top of the stack. This is straightforward but has a limitation in terms of dynamic resizing.
class Stack {
private:
    int top;
    int arr[1000]; // Array size is fixed at 1000
public:
    Stack() { top = -1; }
    void push(int x) {
        if (top >= 999) {
            std::cout << "Stack Overflow\n";
        } else {
            arr[++top] = x;
        }
    }
    void pop() {
        if (top < 0) {
            std::cout << "Stack Underflow\n";
        } else {
            top--;
        }
    }
    int peek() {
        if (top < 0) {
            std::cout << "Stack is Empty\n";
            return -1;
        } else {
            return arr[top];
        }
    }
    bool isEmpty() {
        return top < 0;
    }
};Using a linked list allows the stack to grow dynamically. Each node stores an integer and a pointer to the next node.
struct Node {
    int data;
    Node* next;
};
class Stack {
private:
    Node* top;
public:
    Stack() { top = nullptr; }
    void push(int x) {
        Node* newNode = new Node();
        newNode->data = x;
        newNode->next = top;
        top = newNode;
    }
    void pop() {
        if (!top) {
            std::cout << "Stack Underflow\n";
            return;
        }
        Node* temp = top;
        top = top->next;
        delete temp;
    }
    int peek() {
        if (!top) {
            std::cout << "Stack is Empty\n";
            return -1;
        }
        return top->data;
    }
    bool isEmpty() {
        return top == nullptr;
    }
};The STL provides a stack container, which internally uses either deque or vector to provide dynamic memory management and LIFO operations.
#include <iostream>
#include <stack>
int main() {
    std::stack<int> stk;
    stk.push(10);
    stk.push(20);
    stk.push(30);
    std::cout << "Top element is: " << stk.top() << std::endl;
    stk.pop();
    std::cout << "Top element is: " << stk.top() << std::endl;
    if (stk.empty()) {
        std::cout << "Stack is empty\n";
    } else {
        std::cout << "Stack is not empty\n";
    }
    return 0;
}Push operation adds an element to the top of the stack.
void push(int x) {
    // Implementation specific to array, linked list, or STL stack
}Pop operation removes the top element from the stack.
void pop() {
    // Implementation specific to array, linked list, or STL stack
}Peek operation returns the top element without removing it.
int peek() {
    // Implementation specific to array, linked list, or STL stack
}IsEmpty checks if the stack is empty.
bool isEmpty() {
    // Implementation specific to array, linked list, or STL stack
}