Introduction to Stack in C++

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
This means the last element inserted is the first element removed.

Stacks are widely used in memory management, expression evaluation, recursion, and undo operations.

Introduction to Stack Data Structure - GeeksforGeeks

You may visit the previous lesson: Circular Linked List (With C++ Examples)

๐Ÿ“Œ Definition of Stack

A stack is a collection of elements with two primary operations:

  1. Push โ€“ Add an element to the top of the stack

  2. Pop โ€“ Remove the top element from the stack

Additional operations:

  • Peek/Top โ€“ Access the top element without removing it

  • isEmpty โ€“ Check if the stack is empty

  • isFull โ€“ Check if the stack is full (only for static stacks)

๐Ÿ”น Real-World Use Cases

  • Undo/Redo functionality in editors

  • Browser back/forward buttons

  • Expression evaluation and syntax parsing

  • Recursive function call tracking

1๏ธโƒฃ Static Implementation of Stack

Static stacks are implemented using arrays with a fixed size.

Stack Data Structure and Implementation in Python, Java and C/C++

๐Ÿ’ป C++ Code

#include <iostream>
using namespace std;

#define MAX 5

class Stack {
    int arr[MAX];
    int top;

    public:
    Stack() { top = -1; }

    void push(int x) {
        if (top >= MAX - 1) {
            cout << "Stack Overflow\n";
            return;
        }
        arr[++top] = x;
        cout << x << " pushed into stack\n";
    }

    void pop() {
        if (top < 0) {
            cout << "Stack Underflow\n";
            return;
        }
        cout << arr[top--] << " popped from stack\n";
    }

    int peek() {
        if (top < 0) {
            cout << "Stack is empty\n";
            return -1;
        }
        return arr[top];
    }

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

int main() {
    Stack s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout << "Top element is " << s.peek() << endl;
    s.pop();
    s.pop();
    s.pop();
    s.pop();
    return 0;
}

2๏ธโƒฃ Dynamic Implementation of Stack

Dynamic stacks are implemented using linked lists, allowing flexible memory usage.

Stack Using Linked List in C - GeeksforGeeks

๐Ÿ’ป C++ Code

#include <iostream>
using namespace std;

struct Node {
      int data;
      Node* next;
};

class Stack {
    Node* top;

    public:
    Stack() { top = nullptr; }

    void push(int value) {
        Node* newNode = new Node();
        newNode->data = value;
        newNode->next = top;
        top = newNode;
        cout << value << " pushed into stack\n";
    }

    void pop() {
        if (top == nullptr) {
            cout << "Stack Underflow\n";
            return;
        }
        Node* temp = top;
        top = top->next;
        cout << temp->data << " popped from stack\n";
        delete temp;
    }

    int peek() {
        if (top == nullptr) {
            cout << "Stack is empty\n";
            return -1;
        }
        return top->data;
    }

    bool isEmpty() { return top == nullptr; }
};

int main() {
    Stack s;
    s.push(10);
    s.push(20);
    s.push(30);
    cout << "Top element is " << s.peek() << endl;
    s.pop();
    s.pop();
    s.pop();
    s.pop();
    return 0;
}

๐Ÿ”น Operations Summary

Operation Description Static Stack Dynamic Stack
Push Insert element O(1) O(1)
Pop Remove element O(1) O(1)
Peek Access top O(1) O(1)
isEmpty Check if empty O(1) O(1)
isFull Check if full O(1) Not required

 

โœ… Key Takeaways

  • Stacks are LIFO data structures used in many applications.

  • Static stacks are simple but memory-limited.

  • Dynamic stacks use linked lists and are memory-efficient.

  • Understanding stack operations is crucial for DSA, recursion, and compiler design.

Interview Questions & Answers for Stack

Basic Level

  1. What is a stack?
    A stack is a LIFO (Last In, First Out) data structure where the last element added is the first element removed.

  2. What are the basic operations of a stack

    • Push: Add element to the top

    • Pop: Remove element from the top

    • Peek/Top: View top element

    • isEmpty: Check if stack is empty

    • isFull: Check if stack is full (static stack only)

3. What is the difference between a stack and a queue

  • Stack: LIFO (Last In, First Out)

  • Queue: FIFO (First In, First Out)

4. How is a stack implemented in C++?

  • Static stack: Using arrays with a fixed size

  • Dynamic stack: Using linked lists for flexible memory

5. What is the time complexity of push and pop operations?

O(1) for both static and dynamic stack.

6. What happens if you push to a full static stack?

It causes a Stack Overflow error.

7. What happens if you pop from an empty stack?

It causes a Stack Underflow error.

8. Why are stacks important in programming?

  • Function call tracking (recursion)

  • Expression evaluation (postfix/prefix)

  • Undo/Redo in text editors

  • Browser back/forward history

9. Difference between static and dynamic stack?

Feature ย  ย Static Stack Dynamic Stack
Memory Fixed Dynamic
Overflow Possible Unlikely
Implementation Array Linked List
Size Predefined Flexible

10. Can a dynamic stack be memory-efficient?


Yes, it grows and shrinks according to usage, avoiding memory wastage.

11. Which stack implementation is better for unpredictable data size?

Dynamic stack using linked lists is better because it adapts to data size.

12. Why is stack used in recursion?


Each recursive call is stored on a stack, allowing the program to track function return points.

13. What is the risk of using a static stack in real-time applications?

Stack overflow if the array limit is exceeded, which can crash the application.

Are you interested in developing mobile application development skills? You may visit the link below:

https://onlineskilllab.com/2025/11/18/how-to-build-a-complete-flutter-authentication-system-with-firebase-login-signup-reset-password-dashboard/

If you are learning and enjoying, subscribe to our blog for daily updates.

 

Author

Write A Comment