๐น Introduction
A Stack in Data Structure is one of the most important concepts in computer science. It is widely used in programming, memory management, and algorithm design.
The stack follows a simple but powerful principle called LIFO (Last In, First Out), which means the last element inserted is the first one to be removed.
In this article, you will learn everything about stack, including its definition, operations, examples, and real-world applications.
๐น What is Stack in Data Structure? (Definition)
A Stack is a linear data structure that follows the LIFO (Last In First Out) principle.
๐ In simple words:
The element added last will be removed first.
๐ Real-life example:
Think of a stack of plates โ you can only remove the top plate first.
๐น LIFO Principle Explained
The LIFO (Last In First Out) rule means:
- If you insert 10, then 20, then 30
- The first element to be removed will be 30
Top โ 30
20
10
๐ This behavior makes stack useful in many programming scenarios.
๐น Basic Operations of Stack
A stack supports the following operations:
1. Push Operation
Adds an element to the top of the stack
2. Pop Operation
Removes the top element from the stack
3. Peek (Top)
Returns the top element without removing it
4. isEmpty
Checks whether the stack is empty
๐น Stack Implementation in C
Below is a simple implementation of stack using an array:
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
}
}
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
top--;
}
}
void display() {
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}

๐น Applications of Stack in Real Life
Stacks are widely used in real-world applications:
- Function call management (call stack)
- Expression evaluation (infix, postfix)
- Undo/Redo operations in editors
- Syntax parsing in compilers
- Backtracking algorithms
๐น Advantages of Stack
- Easy to implement
- Efficient memory usage
- Useful in recursion and function calls
๐น Disadvantages of Stack
- Limited size (in array implementation)
- No direct access to middle elements
- Possibility of overflow and underflow
๐น Stack vs Queue (Quick Comparison)
| Feature | Stack | Queue |
|---|---|---|
| Principle | LIFO | FIFO |
| Insertion | Top | Rear |
| Deletion | Top | Front |
๐ Also read: Queue in Data Structure (add internal link here)
๐น Conclusion
A Stack in Data Structure is a simple yet powerful concept that plays a crucial role in programming. Its LIFO behavior makes it ideal for solving problems related to recursion, memory management, and expression evaluation.
If you are preparing for exams like UGC NET or learning programming, mastering stack is essential.