A stack is like a pile of plates: a new plate is put on top, and the plate taken off is always the top one.
Definition
A stack is a linear data structure in which insertion and deletion take place at one end only, called the top. The last element inserted is the first one removed, so a stack follows the LIFO (Last In, First Out) principle. Inserting is called push, and removing is called pop. Other operations are peek (or top), which reads the top element without removing it, isEmpty and isFull. Pushing onto a full stack causes overflow, and popping from an empty stack causes underflow.
Definition: Stack. A linear data structure in which elements are inserted and deleted only at one end, called the top, following the Last In, First Out (LIFO) order.
Array Representation
A stack can be held in an array stack[MAX] with an integer top that gives the index of the top element. Initially top = −1, meaning empty. Push: if top = MAX − 1, report overflow; otherwise increment top and store the item at stack[top]. Pop: if top = −1, report underflow; otherwise return stack[top] and decrement top. Both operations take O(1) time. The weakness is the fixed size.
Linked List Representation
A stack can also be a singly linked list in which the head is the top. Push creates a node and inserts it at the beginning. Pop removes the first node. Both are O(1), and the stack can grow as long as memory is available, so there is no overflow in the usual sense. The cost is an extra pointer per element.
Applications of Stacks
Stacks are used to evaluate and convert expressions, to check balanced parentheses, to support function calls and recursion, for undo in editors, for the back button of a browser, for reversing strings, and in depth-first search and backtracking.
Table 3.1: Stack operations
| Operation | Time | Condition checked |
|---|---|---|
| Push | O(1) | Overflow (array full) |
| Pop | O(1) | Underflow (stack empty) |
| Peek | O(1) | Stack not empty |
| isEmpty | O(1) | top = −1 or head = NULL |
Example 3.1: Push and pop
Problem: On an empty stack of size 4, perform push 10, push 20, push 30, pop, push 40, push 50, push 60. Show the result.
Solution:
- After three pushes: 10, 20, 30 (top 30).
- Pop removes 30: 10, 20.
- Push 40, push 50: 10, 20, 40, 50. The stack is full.
- Push 60 causes overflow.
Result: The stack holds 10, 20, 40, 50 with top 50, and the last push overflows.
#include <stdio.h>
#define MAX 4
int st[MAX], top = -1;
void push(int v) {
if (top == MAX - 1) { printf("Overflow on %d\n", v); return; }
st[++top] = v;
}
int pop(void) {
if (top == -1) { printf("Underflow\n"); return -1; }
return st[top--];
}
int main(void) {
push(10); push(20); push(30);
printf("popped %d\n", pop());
push(40); push(50); push(60);
for (int i = top; i >= 0; i--) printf("%d ", st[i]);
printf("\n");
return 0;
}
Output
popped 30
Overflow on 60
50 40 20 10
Common mistakes
- Not checking for overflow before push or underflow before pop.
- Writing st[top++] = v with top starting at −1, which uses the wrong index.
- Taking the tail of a linked list as the top, which makes pop O(n).
Key takeaways
- A stack is LIFO, with push and pop at the top.
- Array: top starts at −1; full when top = MAX − 1.
- Linked list: the head is the top.
- All stack operations are O(1).
Practice questions
- What does LIFO mean?
- When does stack overflow occur?
- Give two applications of stacks.
Answers to the practice questions
- Last In, First Out: the last element pushed is the first popped.
- When a push is attempted on a full stack.
- Expression evaluation and function calls (also undo, parenthesis checking).
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III
