03
Stacks and Queues
Chapter 3 • Intermediate
50 min
Stacks and Queues
Stacks and queues are linear data structures that follow specific insertion and deletion rules.
Stack
Stack is a LIFO (Last In First Out) data structure.
Stack Operations
Basic Operations:
- push(x): Insert element x at top - O(1)
- pop(): Remove and return top element - O(1)
- peek()/top(): Return top element without removing - O(1)
- isEmpty(): Check if stack is empty - O(1)
- size(): Return number of elements - O(1)
Stack Implementation
Using Array
c.js
#define MAX 100
int stack[MAX];
int top = -1;
void push(int x) {
if (top == MAX - 1) {
printf("Stack Overflow");
return;
}
stack[++top] = x;
}
int pop() {
if (top == -1) {
printf("Stack Underflow");
return -1;
}
return stack[top--];
}
int peek() {
if (top == -1) return -1;
return stack[top];
}
Using Linked List
c.js
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int x) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = top;
top = newNode;
}
int pop() {
if (top == NULL) {
printf("Stack Underflow");
return -1;
}
struct Node* temp = top;
int data = top->data;
top = top->next;
free(temp);
return data;
}
Stack Applications
- Expression Evaluation
- Infix to postfix conversion
- Postfix evaluation
- Parenthesis matching
- Function Calls
- Recursion uses call stack
- Stack frame management
- Undo/Redo Operations
- Text editors
- Browsers
- Backtracking
- DFS implementation
- Maze solving
Expression Evaluation
Infix Notation: A + B (operator between operands)
Postfix Notation: AB+ (operator after operands)
Prefix Notation: +AB (operator before operands)
Infix to Postfix Algorithm:
- Scan expression left to right
- If operand, output it
- If '(', push to stack
- If ')', pop until '('
- If operator:
- Pop operators with higher/equal precedence
- Push current operator
- Pop remaining operators
Postfix Evaluation:
- Scan expression left to right
- If operand, push to stack
- If operator:
- Pop two operands
- Apply operator
- Push result
- Final result is stack top
Queue
Queue is a FIFO (First In First Out) data structure.
Queue Operations
Basic Operations:
- enqueue(x): Insert element x at rear - O(1)
- dequeue(): Remove and return front element - O(1)
- front(): Return front element without removing - O(1)
- isEmpty(): Check if queue is empty - O(1)
- size(): Return number of elements - O(1)
Queue Implementation
Using Array (Circular Queue)
c.js
#define MAX 100
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int x) {
if ((rear + 1) % MAX == front) {
printf("Queue Full");
return;
}
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
queue[rear] = x;
}
int dequeue() {
if (front == -1) {
printf("Queue Empty");
return -1;
}
int data = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
return data;
}
Using Linked List
c.js
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int x) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
int dequeue() {
if (front == NULL) {
printf("Queue Empty");
return -1;
}
struct Node* temp = front;
int data = front->data;
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
return data;
}
Queue Variations
1. Circular Queue
- Rear wraps around to front
- Efficient space utilization
- Prevents overflow
2. Priority Queue
- Elements dequeued by priority
- Can be implemented using heap
- Used in scheduling algorithms
3. Deque (Double-ended Queue)
- Insert/delete from both ends
- Can be used as stack or queue
Queue Applications
- BFS (Breadth-First Search)
- Graph traversal
- Level-order tree traversal
- Scheduling
- Process scheduling
- Task scheduling
- Buffering
- I/O buffering
- Network packet queuing
- Print Spooling
- Print job management
Stack vs Queue
| Feature | Stack | Queue |
|---|---|---|
| Order | LIFO | FIFO |
| Insert | Top | Rear |
| Delete | Top | Front |
| Applications | Expression eval, recursion | BFS, scheduling |
GATE CS Important Points
- Operations: Know all operation complexities
- Implementation: Understand both array and linked list implementations
- Applications: Expression evaluation, BFS, recursion
- Parenthesis Matching: Stack-based solution
- Circular Queue: Understand wrap-around logic
Practice Tips
- Implement Both: Code stack and queue from scratch
- Expression Evaluation: Practice infix to postfix conversion
- Parenthesis Matching: Solve using stack
- BFS Implementation: Use queue for graph traversal
- Previous Year Questions: Solve GATE stack/queue questions