Stack in Data Structure: Operations, Implementation and Applications
Understand Stack in simple language with LIFO principle, Push, Pop, Peek, pictorial diagrams, dry runs, array and Linked List implementation, C programs and exam questions.
Imagine you have a pile of books on your study table. You place one book on top of another. When you want to remove a book, the easiest book to pick up is the one placed on top.
This is exactly how a Stack works in Data Structure. The last element that enters the Stack is the first element that comes out.
Many students become confused between Push, Pop and Peek. Some also struggle to understand how the TOP variable changes after each operation.
In this chapter, we will understand the complete working of a Stack using simple examples before moving to algorithms and C programs.
What is a Stack in Data Structure?

A Stack is a linear Data Structure in which elements are inserted and removed from the same end.
This end is called the TOP of the Stack. Unlike an ordinary list, a Stack does not allow removal from any arbitrary position through its standard operations.
Basic Stack Diagram
In this example, the values entered in the order 10, 20 and 30.
But removal happens in the reverse order: 30, 20 and 10.
Important Features of a Stack
Linear Structure
Elements are logically arranged in a sequence.
One Active End
Standard insertion and deletion happen only at TOP.
LIFO Order
The newest element is removed before older elements.
Two Common Implementations
A Stack can use a fixed-capacity array or dynamically linked nodes.
LIFO Principle in Stack
LIFO stands for Last In, First Out. It means the element inserted last will be removed first.
Think of a stack of plates in a restaurant. The plate placed last on top is normally the first one taken out.
LIFO Working Example
After these operations, the Stack contains 10, 20 and 30.
If we perform Pop, element 30 will be removed first.
Remember the sentence: "Last Comes, First Goes."
This one line explains the complete LIFO principle.
Basic Operations of Stack in Data Structure
Stack operations define how elements are inserted, removed and accessed.
The most important operations for exams are Push, Pop and Peek. Two additional checks are isEmpty and isFull.
| Operation | Meaning |
|---|---|
| Push | Insert an element at TOP |
| Pop | Remove and return TOP element |
| Peek | Read TOP without removing it |
| isEmpty | Check whether Stack has no elements |
| isFull | Check whether fixed-capacity Stack is full |
| Display | Show the Stack elements |
1. Push Operation in Stack
Push is the operation used to insert a new element at the TOP of a Stack.
Suppose our Stack contains 10 and 20. Now we want to insert 30.
After Push(30), the new value 30 becomes TOP. The other values remain in the Stack.
Push Algorithm Using Array
IF TOP == MAX - 1
PRINT "Stack Overflow"
ELSE
TOP = TOP + 1
STACK[TOP] = ITEM
END IF
2. Pop Operation in Stack
Pop is the operation used to remove and return the element at TOP.
Suppose the Stack contains 10, 20 and 30.
Pop Algorithm Using Array
IF TOP == -1
PRINT "Stack Underflow"
ELSE
ITEM = STACK[TOP]
TOP = TOP - 1
RETURN ITEM
END IF
3. Peek Operation in Stack
Peek returns the value present at TOP without deleting it.
After Peek, the Stack remains unchanged. The TOP is still 30.
Peek Algorithm
IF TOP == -1
PRINT "Stack is Empty"
ELSE
RETURN STACK[TOP]
END IF
4. isEmpty Operation
isEmpty checks whether the Stack contains any elements.
In the zero-based array implementation, TOP is initially set to -1.
5. isFull Operation
isFull checks whether a fixed-size array Stack has reached capacity.
For an array of size MAX, the final valid index is MAX - 1.
For example, if MAX = 5, valid array indices are 0 to 4. When TOP = 4, the Stack is full.
6. Display Operation
Display visits the active Stack elements, commonly from TOP towards the bottom.
FOR I = TOP DOWN TO 0
PRINT STACK[I]
END FOR
For a Stack containing 10, 20, 30, the display order from TOP is 30, 20, 10.
Stack Operations: Complete Step-by-Step Dry Run
Many university exams ask students to calculate the final Stack after a sequence of Push and Pop operations.
Let us take a Stack with capacity 3 and perform eight operations.
| Step | Operation | TOP Index | Stack (Bottom → Top) | Result |
|---|---|---|---|---|
| 1 | Push(10) | 0 | 10 | Inserted |
| 2 | Push(20) | 1 | 10, 20 | Inserted |
| 3 | Push(30) | 2 | 10, 20, 30 | Inserted |
| 4 | Push(40) | 2 | 10, 20, 30 | Overflow |
| 5 | Peek() | 2 | 10, 20, 30 | Returns 30 |
| 6 | Pop() | 1 | 10, 20 | Returns 30 |
| 7 | Push(40) | 2 | 10, 20, 40 | Inserted |
| 8 | Pop() | 1 | 10, 20 | Returns 40 |
How to Solve Stack Dry Run Questions?
For an empty array Stack, write TOP = -1.
Increase TOP after checking whether capacity is available.
Record the removed value and decrease TOP.
A failed operation does not change the Stack.
Show the values and the final TOP position clearly.
Stack Overflow and Stack Underflow
Overflow and Underflow are two important conditions students must understand.
Stack Overflow
Overflow occurs when we try to Push into a Stack that cannot accept another element.
In a fixed-size array Stack, this happens when TOP = MAX - 1.
Stack Underflow
Underflow occurs when we try to Pop an element from an empty Stack.
In an array Stack, this happens when TOP = -1.
Stack Implementation Using Array
In Array Implementation, Stack elements are stored inside an array and a variable called TOP tracks the final active element.
This is one of the easiest ways to implement a Stack in C.
Array Representation
| Array Index | Value | Status |
|---|---|---|
| 0 | 10 | Bottom |
| 1 | 20 | Middle |
| 2 | 30 | TOP |
| 3 | Unused | Available |
| 4 | Unused | Available |
The Stack has three elements, so TOP is at index 2.
Complete C Program: Stack Using Array
This program performs Push, Pop, Peek and Display operations. It also checks for Stack overflow and underflow.
#include <stdio.h>
#define CAPACITY 5
typedef struct {
int data[CAPACITY];
int top;
} Stack;
void init(Stack *s) {
s->top = -1;
}
int push(Stack *s, int value) {
if (s->top == CAPACITY - 1)
return 0;
s->data[++s->top] = value;
return 1;
}
int pop(Stack *s, int *removed) {
if (s->top == -1)
return 0;
*removed = s->data[s->top--];
return 1;
}
int peek(const Stack *s, int *value) {
if (s->top == -1)
return 0;
*value = s->data[s->top];
return 1;
}
void display(const Stack *s) {
for (int i = s->top; i >= 0; i--)
printf("%d ", s->data[i]);
putchar('\n');
}
int main(void) {
Stack s;
int value;
init(&s);
if (!push(&s, 10) ||
!push(&s, 20) ||
!push(&s, 30)) {
puts("Stack overflow");
return 1;
}
printf("After push: ");
display(&s);
if (peek(&s, &value))
printf("Top value: %d\n", value);
if (pop(&s, &value))
printf("Popped: %d\n", value);
printf("After pop: ");
display(&s);
return 0;
}
Output
After push: 30 20 10
Top value: 30
Popped: 30
After pop: 20 10
Explanation of the Program
init() sets TOP to -1, meaning the Stack is empty.
Push inserts 10, 20 and 30. Every successful insertion increases TOP.
Peek reads 30 without removing it.
Pop removes 30 and decreases TOP.
The Stack now contains 20 and 10 from TOP towards the bottom.
Advantages of Array Stack
- Simple to understand and implement.
- Array elements are stored contiguously.
- Push, Pop and Peek take O(1) time.
- No separate pointer field is needed for each element.
- Good cache locality in many implementations.
Disadvantages of Array Stack
- A fixed array has limited capacity.
- Push fails when the array is full.
- Unused array positions reserve storage.
- Increasing a fixed array's capacity may require creating a larger array.
Stack Implementation Using Linked List
A Stack can also be created using a Singly Linked List.
Every node contains data and a pointer to the next node. We use HEAD as the TOP pointer.
For Push, we insert a new node at the beginning. For Pop, we remove the first node.
If you need a revision of nodes and pointers, read Linked List in Data Structure: Types, Operations and Examples .
Linked List Stack Diagram
Push Operation Using Linked List
- Create a new node.
- Store the new value.
- Connect its next pointer to the current TOP.
- Update TOP to the new node.
NEW = CREATE_NODE(ITEM)
IF NEW == NULL
PRINT "Allocation Failed"
ELSE
NEW.next = TOP
TOP = NEW
END IF
Pop Operation Using Linked List
- Check whether TOP is NULL.
- Save the first node.
- Move TOP to the next node.
- Release the removed node.
- Return the removed value.
IF TOP == NULL
PRINT "Stack Underflow"
ELSE
TEMP = TOP
ITEM = TEMP.data
TOP = TOP.next
DELETE TEMP
RETURN ITEM
END IF
Complete C Program: Stack Using Linked List
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
int push(Node **top, int value) {
Node *node = malloc(sizeof *node);
if (node == NULL)
return 0;
node->data = value;
node->next = *top;
*top = node;
return 1;
}
int pop(Node **top, int *removed) {
if (*top == NULL)
return 0;
Node *old = *top;
*removed = old->data;
*top = old->next;
free(old);
return 1;
}
int peek(const Node *top, int *value) {
if (top == NULL)
return 0;
*value = top->data;
return 1;
}
void display(const Node *top) {
while (top != NULL) {
printf("%d ", top->data);
top = top->next;
}
putchar('\n');
}
int main(void) {
Node *top = NULL;
int value;
if (!push(&top, 10) ||
!push(&top, 20) ||
!push(&top, 30)) {
puts("Memory allocation failed");
while (pop(&top, &value)) {
}
return 1;
}
printf("After push: ");
display(top);
if (peek(top, &value))
printf("Top value: %d\n", value);
if (pop(&top, &value))
printf("Popped: %d\n", value);
printf("After pop: ");
display(top);
while (pop(&top, &value)) {
}
return 0;
}
Output
After push: 30 20 10
Top value: 30
Popped: 30
After pop: 20 10
Explanation of Linked List Implementation
Push
Allocates a node and makes it the new TOP.
Pop
Removes the first node and updates TOP.
Peek
Reads the value stored in the TOP node.
Display
Traverses all nodes from TOP to NULL.
Advantages of Linked List Stack
- Nodes can be allocated when required.
- No fixed array-capacity limit is necessary.
- Push and Pop update only the TOP links.
- Memory for nodes can be released after deletion.
Disadvantages of Linked List Stack
- Each node requires extra pointer storage.
- Dynamic memory allocation has overhead.
- Incorrect pointer handling can cause errors.
- New node allocation may fail when memory is unavailable.
Array Stack vs Linked List Stack
Both implementations follow the same LIFO principle. The difference is how the Stack elements are stored.
| Feature | Array Stack | Linked List Stack |
|---|---|---|
| Storage | Array elements | Connected nodes |
| TOP | Array index | Pointer to first node |
| Capacity | Fixed in basic implementation | Grows while allocation succeeds |
| Memory Layout | Contiguous | Nodes may be separate |
| Extra Memory | Reserved capacity | Pointer per node |
| Push | O(1) | O(1) link changes |
| Pop | O(1) | O(1) |
| Typical Use | Known capacity | Changing number of elements |
Applications of Stack in Data Structure
Stack is not only an exam topic. It is a useful structure in programming, algorithms and many everyday applications.
Function Calls and Recursion
Programs commonly use a call stack to manage active function calls, return information and local execution context.
Undo and Redo
Text editors can use stacks of actions to undo recent changes and restore them.
Expression Evaluation
Stacks are used for postfix expression evaluation and expression conversion.
Balanced Parentheses
A Stack can check whether brackets in an expression are correctly matched.
Depth-First Search
Iterative DFS uses a Stack to manage nodes that still need to be explored.
Backtracking
Stacks can store previous choices while exploring solutions to problems.
Example: Undo Operation
Imagine a student editing a paragraph in a text editor.
The most recent action is Type C. Therefore, the first Undo reverses Type C.
This follows the LIFO principle. Real editors may use more complex histories, but Stack is a useful model for basic Undo operations.
Example: Balanced Parentheses Using Stack
Consider this expression:
Whenever we find an opening bracket, we Push it into the Stack.
Whenever we find a closing bracket, we check and Pop the matching opening bracket.
Dry Run
| Symbol | Action | Bracket Stack |
|---|---|---|
| ( | Push | ( |
| A + B | Ignore other symbols | ( |
| ) | Match and Pop | Empty |
| * | Ignore | Empty |
| ( | Push | ( |
| C - D | Ignore other symbols | ( |
| ) | Match and Pop | Empty |
Algorithm for Balanced Brackets
START
CREATE EMPTY STACK
FOR EACH SYMBOL IN EXPRESSION
IF SYMBOL IS OPENING BRACKET
PUSH SYMBOL
ELSE IF SYMBOL IS CLOSING BRACKET
IF STACK IS EMPTY
RETURN NOT BALANCED
END IF
OPEN = POP()
IF OPEN DOES NOT MATCH SYMBOL
RETURN NOT BALANCED
END IF
END IF
END FOR
IF STACK IS EMPTY
RETURN BALANCED
ELSE
RETURN NOT BALANCED
END IF
STOP
Time and Space Complexity of Stack
Push, Pop and Peek work only at the TOP of the Stack. They do not need to visit every stored element.
| Operation | Array Stack | Linked Stack |
|---|---|---|
| Push | O(1) | O(1) link operation |
| Pop | O(1) | O(1) |
| Peek | O(1) | O(1) |
| isEmpty | O(1) | O(1) |
| isFull | O(1) for fixed array | Not a fixed-capacity check |
| Display | O(n) | O(n) |
Space Complexity
Fixed Array Stack
Reserves storage for K elements, whether all positions are used or not.
Linked List Stack
Uses storage for n nodes, including their pointer fields.
Here, K is the array capacity and n is the number of elements currently stored.
Difference Between Stack and Queue
Stack and Queue are both linear Data Structures, but their element-removal orders are different.
| Basis | Stack | Queue |
|---|---|---|
| Principle | LIFO | FIFO |
| Insertion | Push at TOP | Enqueue at REAR |
| Deletion | Pop from TOP | Dequeue from FRONT |
| Ends Used | One active end | Usually two ends |
| Example | Stack of plates | People in a line |
| Application | Undo, DFS, call stack | Task scheduling, buffering |
Advantages and Disadvantages of Stack
Advantages
- Simple LIFO behaviour.
- Fast Push and Pop.
- Useful for reversing recent actions.
- Supports recursive and iterative algorithms.
- Easy implementation using arrays or nodes.
Disadvantages
- Restricted access to TOP.
- No general direct access to arbitrary positions.
- Fixed array implementations have capacity limits.
- Incorrect Pop on empty Stack causes underflow.
- Linked implementations need extra pointers.
Common Mistakes Students Make in Stack
1. Confusing LIFO with FIFO
Stack follows LIFO. Queue follows FIFO.
2. Wrong Initial TOP
In the common zero-based array implementation, empty TOP is -1.
3. Confusing Pop and Peek
Pop removes the top element. Peek only reads it.
4. Forgetting Overflow Check
A fixed array Stack must check whether capacity is available before Push.
5. Popping an Empty Stack
Pop must check whether the Stack contains any elements.
6. Wrong Linked List End
Stack Push and Pop should normally operate at the HEAD of a Singly Linked List.
Easy Memory Tricks for Stack
LIFO
Last In, First Out. The last entered item leaves first.
PPP Rule
Push = Put
Pop = Pull out
Peek = Preview
TOP Movement
Push = TOP increases.
Pop = TOP decreases.
(Array implementation)
Overflow vs Underflow
Overflow = Too full.
Underflow = Nothing to remove.
Important Stack Exam Questions With Answers
Q1. What is Stack in Data Structure?
Stack is a linear Data Structure that follows the Last In, First Out principle. Elements are inserted and deleted at one end called TOP.
Q2. What is LIFO?
LIFO stands for Last In, First Out. The element inserted last into the Stack is removed first.
Q3. What is Push Operation?
Push is the operation used to insert a new element at the TOP of a Stack.
Q4. What is Pop Operation?
Pop removes and returns the current TOP element of a Stack.
Q5. Define Stack Overflow and Underflow.
Overflow occurs when an insertion cannot be performed because Stack capacity is exhausted. Underflow occurs when we try to remove an element from an empty Stack.
Q6. Explain Push and Pop Operations With Algorithms.
Push and Pop are the two basic Stack operations.
Push: Inserts an element at TOP after checking whether sufficient space exists.
IF TOP == MAX - 1
PRINT "Overflow"
ELSE
TOP = TOP + 1
STACK[TOP] = ITEM
END IF
Pop: Removes and returns the TOP element after checking that the Stack is not empty.
IF TOP == -1
PRINT "Underflow"
ELSE
ITEM = STACK[TOP]
TOP = TOP - 1
RETURN ITEM
END IF
Both operations normally take O(1) time in a fixed-capacity array Stack.
Q7. Explain Stack Implementation Using Array.
An array-based Stack stores elements in a fixed-size array. A variable called TOP keeps the index of the final active element.
Initially, TOP = -1. During Push, TOP increases and the new value is stored. During Pop, the top value is removed and TOP decreases.
The main advantage is simple implementation and constant-time basic operations. The limitation is fixed capacity.
Q8. Explain Stack Implementation Using Linked List.
In Linked List implementation, a Stack is formed using dynamically allocated nodes.
Every node contains data and a next pointer. TOP points to the first node.
Push creates a new node and inserts it at the beginning. Pop removes the first node, updates TOP and releases its memory.
This method allows flexible growth while memory allocation succeeds, but each node needs extra pointer storage.
Q9. Explain Stack, Its Operations, Implementation and Applications.
A Stack is a linear Data Structure in which insertion and deletion take place at one end called TOP. It follows the Last In, First Out principle.
Main Operations
- Push inserts an element at TOP.
- Pop removes the top element.
- Peek returns TOP without removing it.
- isEmpty checks for an empty Stack.
- isFull checks a fixed array Stack's capacity.
Array Implementation
Elements are stored in an array and TOP stores the active top element's index. Initially, TOP is -1. Push increases TOP and Pop decreases it.
Linked List Implementation
Elements are stored as connected nodes. TOP points to the first node. Push adds a new node at the beginning, while Pop removes it.
Applications
- Function call management
- Undo and Redo operations
- Expression evaluation
- Balanced parentheses checking
- Depth-First Search
- Backtracking algorithms
Advantages
Stack is simple to implement and provides efficient Push, Pop and Peek operations.
Limitations
Only TOP is directly accessible through standard operations. A fixed array Stack can overflow when capacity is reached.
Conclusion: Stack is an important linear Data Structure used in many algorithms and program-execution activities because of its LIFO behaviour.
Stack Practice Questions and MCQs
Which principle does Stack follow?
A. FIFO
B. LIFO
C. Random Order
D. Priority Order
Show Answer
B. LIFO — Last In, First Out.
Which operation removes the TOP element?
A. Push
B. Peek
C. Pop
D. isEmpty
Show Answer
C. Pop
What is the initial TOP value in the common zero-based array Stack?
A. 0
B. 1
C. -1
D. MAX
Show Answer
C. -1
What is the time complexity of Peek?
A. O(1)
B. O(n)
C. O(log n)
D. O(n²)
Show Answer
A. O(1)
Find the final Stack.
Starting with an empty Stack:
Show Answer
Pop removes 10. Push then adds 15. Peek returns 15 without removing it. Final Stack (bottom to top) = 5, 15. TOP value = 15.
Quick Revision Before Semester Exam
These are the most important Stack points for last-minute revision.
Linear Data Structure
LIFO
Active end of Stack
Insert an element
Remove an element
Read TOP without removal
TOP = -1
TOP = MAX - 1
O(1)
O(n)
Frequently Asked Questions
What is Stack in Data Structure?
A Stack is a linear Data Structure that follows the Last In, First Out principle. Insertion and deletion take place at TOP.
Why does Stack follow LIFO?
Standard Stack operations access only TOP. Therefore, the most recently inserted element is removed first.
What is the difference between Push and Pop?
Push inserts a new element at TOP, while Pop removes and returns the element currently at TOP.
What is the difference between Pop and Peek?
Pop removes the top element. Peek reads the top element without removing it.
Can Stack be implemented using Linked List?
Yes. A Stack can use a Singly Linked List where TOP points to the first node. Push and Pop occur at the beginning.
What is the time complexity of Push and Pop?
Basic Push and Pop operations take O(1) time for a fixed array Stack with available capacity, and involve O(1) link changes in a linked Stack.
What is Stack Overflow?
Stack Overflow occurs when an insertion cannot be completed because the Stack cannot accept another element. For a fixed-size array Stack, this happens when it is full.
What are real-life applications of Stack?
Stacks are used in function calls, undo operations, expression evaluation, balanced parentheses, DFS and backtracking.
Conclusion
Stack in Data Structure becomes easy when we understand one simple rule: the last element inserted is the first element removed.
The most important operations are Push, Pop and Peek. Push inserts an element, Pop removes the top element, and Peek reads it without removing it.
Stack can be implemented using an array or Linked List. Array implementation uses a TOP index, while Linked List implementation uses a TOP pointer.
It is widely used in function calls, expression processing, undo operations and backtracking algorithms.
Final Memory Trick: Stack means LIFO, Push means Put, Pop means Pull out, and Peek means Preview.
Once these basics are clear, Stack algorithms, C programs and dry-run questions become much easier to solve in semester exams.
