Stack
What is a Stack?
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This
means the element that is inserted last will be removed first. It works just like a stack of
plates: the last plate placed on the top is the first one taken off.
Key Characteristics of a Stack
Linear structure – elements are arranged in a sequence.
LIFO principle – last inserted element is accessed first.
Single access point – operations are performed only at the top.
Efficient operations – insertion and deletion are O(1).
Core Stack Operations
1. push(x)
Adds an element x to the top of the stack.
2. pop()
Removes and returns the top element of the stack.
3. peek() or top()
Returns the top element without removing it.
4. isEmpty()
Checks if the stack contains zero elements.
5. isFull() (for fixed-size array implementation)
Checks if the stack has reached maximum capacity.
Types of Stack Implementations
1. Array-Based Stack
Fast and simple.
Fixed size (static memory allocation).
2. Linked List-Based Stack
No fixed size (dynamic memory allocation).
Ideal when memory usage is unpredictable.
3. STL Stack in C++
C++ provides built-in stack in <stack> library.
How Stack Works Internally
The stack maintains an internal pointer called top.
Initially, top = -1 for empty stack.
Push increases top: top++.
Pop decreases top: top--.
Underflow occurs when popping from an empty stack.
Overflow occurs when pushing into a full stack.
Real-Life Applications of Stack
1. Undo/Redo operations in text editors
Each action is pushed; undo pops the recent action.
2. Browser History
Back button uses stack of visited pages.
3. Function Call Stack
Every function call is pushed into the stack frame.
4. Expression Evaluation
Used for:
Infix to postfix conversion
Postfix evaluation
5. Balanced Parentheses Checking
Using stack to match brackets.
6. DFS (Depth-First Search) in graphs
Stack stores nodes to be visited.
7. Stack Representation Diagram
| |
| 50 | <-- Top
| 30 |
| 20 |
| 10 |
---------
Top element = 50
Stack Using Array – Full C++ Code
#include <iostream>
using namespace std;
class Stack {
int top;
int arr[100]; // fixed size
public:
Stack() { top = -1; }
void push(int x) {
if (top >= 99) {
cout << "Stack Overflow" << endl;
return;
}
arr[++top] = x;
}
int pop() {
if (top < 0) {
cout << "Stack Underflow" << endl;
return -1;
}
return arr[top--];
}
int peek() {
if (top < 0) {
cout << "Stack is Empty" << endl;
return -1;
}
return arr[top];
}
bool isEmpty() {
return (top < 0);
}
};
int main() {
Stack s;
[Link](10);
[Link](20);
[Link](30);
cout << "Top Element: " << [Link]() << endl;
cout << "Popped: " << [Link]() << endl;
cout << "Popped: " << [Link]() << endl;
cout << "Top Element: " << [Link]() << endl;
return 0;
}
Stack Using Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
class Stack {
Node* top;
public:
Stack() { top = nullptr; }
void push(int x) {
Node* temp = new Node(x);
temp->next = top;
top = temp;
}
int pop() {
if (!top) {
cout << "Stack Underflow" << endl;
return -1;
}
int val = top->data;
Node* temp = top;
top = top->next;
delete temp;
return val;
}
int peek() {
if (!top) {
cout << "Stack is Empty" << endl;
return -1;
}
return top->data;
}
bool isEmpty() {
return (top == nullptr);
}
};
int main() {
Stack s;
[Link](100);
[Link](200);
[Link](300);
cout << "Top Element: " << [Link]() << endl;
cout << "Popped: " << [Link]() << endl;
cout << "Top Element: " << [Link]() << endl;
return 0;
}
Advantages of Stack
Very simple and efficient.
Fast operations (O(1)).
Useful for managing function calls.
Helps solve complex problems like expression parsing.
Disadvantages of Stack
Limited access (only top element can be accessed).
Fixed size in array implementation.
May overflow if size is small.