BCS301 · DATA STRUCTURE · Unit 2
Stacks and Queues
Unit Scope & Exam Weightage: Unit 2 focuses on linear restricted data structures: stack ADT and operations, expression conversion/evaluation, recursion, queue types, and priority queue. AKTU heavily asks for infix to postfix/prefix conversion, postfix evaluation, stack using array/linked list, circular queue operations, and recursion/Tower of Hanoi.
Front Page: Importance-Wise Topic Roadmap (PYQ Frequency × Marks)
| Priority | Topic (Exact Syllabus) | PYQs Asked | Marks Weightage | Tag |
|---|---|---|---|---|
| 1 | Conversion of Infix to prefix and postfix, Evaluation of postfix expression | 4 (2022-23, 2023-24, 2024-25) |
7m × 4 = 28m | Survival Mode |
| 2 | Primitive stack operations, Array and linked representation of stacks | 3 (2023-24, 2024-25) |
7m × 2 + 2m = 16m | High-Yield |
| 3 | Queues, operations on queues, Circular queues, Dequeue, Priority queue | 3 (2022-23, 2023-24, 2024-25) |
7m × 2 + 2m = 16m | High-Yield |
| 4 | Recursion, Tower of Hanoi Problem, Tail recursion, Removal of recursion | 2 (2023-24, 2024-25) |
7m + 2m = 9m | Core Concept |
| 5 | Stack ADT and applications, Balanced parenthesis | 1 (2023-24) |
7m | Core Concept |
2-Hour Last-Night Survival Checklist:
- Infix to postfix: Use operator stack; precedence (^ highest, then */ , then +-) and associativity (^ right-to-left). Show stack, input symbol, and output at every step.
- Postfix evaluation: Scan left to right; push operands, on operator pop two operands (second popped is left operand), apply operator, push result.
- Circular queue: Use
(rear+1)%size==frontfor full andfront==-1for empty; after dequeue of last element resetfront=rear=-1. - Recursion: Write base case, recursive case, and stack unwinding for factorial/Tower of Hanoi; TOH moves = .
- Stack applications: Be ready for balanced parentheses, function call stack, undo/redo, and expression parsing.
1. Stack ADT and Primitive Operations
Stack: A linear data structure that follows LIFO (Last In, First Out). Insertion and deletion occur only at one end called top.
1.1 Stack ADT Operations
| Operation | Description | Array Condition | Time |
|---|---|---|---|
push(x) |
Insert x at top | Overflow if top==MAX-1 |
|
pop() |
Remove and return top | Underflow if top==-1 |
|
peek()/top() |
Return top without removing | Underflow if empty | |
isEmpty() |
Check if stack empty | top==-1 |
|
isFull() |
Check if array stack full | top==MAX-1 |
1.2 Array and Linked Representation
Array stack: int stack[MAX]; int top=-1; Easy, but fixed size.
Linked stack: Top is head of linked list. Push inserts at head; pop deletes head. No fixed size limit (until memory exhausted).
void push(int x){ Node* n=malloc(sizeof(Node)); n->data=x; n->next=top; top=n; }
int pop(){ if(!top) error; int x=top->data; Node* t=top; top=top->next; free(t); return x; }
1.3 Applications of Stack
- Function call/return and recursion stack.
- Expression conversion and evaluation.
- Balanced parentheses and syntax checking.
- Undo/redo, browser backtracking.
- DFS, backtracking algorithms.
After-Topic PYQ:
- 2023-24 (Sec A Q1b, 2 Marks): "Write the underflow and overflow conditions in a stack."
2. Expression Conversion and Evaluation
2.1 Notations
| Notation | Form | Example () | Parentheses Needed |
|---|---|---|---|
| Infix | operator between operands | Yes | |
| Prefix (Polish) | operator before operands | No | |
| Postfix (Reverse Polish) | operator after operands | No |
2.2 Infix to Postfix (Stack Algorithm)
- Scan infix left to right.
- Operand: append to output.
(: push to stack.): pop to output until(.- Operator: pop operators from stack to output while stack top has higher precedence, or same precedence and operator is left-associative; then push current operator.
- After scan, pop remaining operators.
Precedence: ^ = 3 (right associative), * / = 2, + - = 1.
Worked Example: Convert A + B * C ^ D - E to postfix.
2.3 Postfix Evaluation
Scan left to right: push operands; on operator, pop , pop , compute , push result. Final stack value is answer.
Worked Example: Evaluate postfix 2 3 1 * + 9 -.
Result = -4.
2.4 Balanced Parenthesis
Push opening brackets. For closing bracket, check stack not empty and top matches. At end, stack must be empty.
After-Topic PYQ:
- 2024-25 (Sec C Q2b, 7 Marks): "Convert the infix expression A+(BC-(D/E^F)*G)H into postfix expression. Show stack status."
- 2023-24 (Sec B Q2d, 7 Marks): "Write an algorithm to evaluate a postfix expression and evaluate 5 6 2 + * 12 4 / -."
3. Recursion and Tower of Hanoi
Recursion: A technique where a function calls itself to solve a smaller instance of the same problem. It must have a base case to terminate and a recursive case that progresses toward the base case.
3.1 Tail Recursion
A recursive call is tail recursive if it is the last action of the function. Tail recursion can be optimized to iteration by compiler. Non-tail recursion requires stack frames until return.
3.2 Tower of Hanoi
Problem: Move disks from source to destination using auxiliary peg; only one disk moved at a time; larger disk cannot be placed on smaller.
Recursive solution:
- Move disks from Source to Auxiliary.
- Move th disk from Source to Destination.
- Move disks from Auxiliary to Destination.
3.3 Removal of Recursion
Recursion can be replaced by iteration using an explicit stack to simulate call stack. Useful when recursion depth is large or language/runtime has limited stack.
After-Topic PYQ:
- 2023-24 (Sec C Q2b, 7 Marks): "Explain recursion. Write recursive solution for Tower of Hanoi and compute moves for 4 disks."
4. Queues, Circular Queue, Deque, Priority Queue
Queue: A linear data structure following FIFO (First In, First Out). Insertion occurs at rear and deletion occurs at front.
4.1 Simple Queue (Array)
- Enqueue: if
rear==MAX-1overflow; elserear++; queue[rear]=x. - Dequeue: if
front==-1 || front>rearunderflow; elsex=queue[front++]. - Drawback: unused space at beginning after dequeues.
4.2 Circular Queue
Overcomes simple queue space waste by wrapping around.
Circular Queue Conditions:
4.3 Deque and Priority Queue
| Type | Insertion | Deletion | Use |
|---|---|---|---|
| Input-restricted deque | One end | Both ends | Specialized buffers. |
| Output-restricted deque | Both ends | One end | Specialized buffers. |
| Priority queue | By priority | Highest/lowest priority first | CPU scheduling, Dijkstra, Huffman. |
Priority queue can be implemented by sorted array/list or heap (efficient).
After-Topic PYQ:
- 2024-25 (Sec B Q2c, 7 Marks): "Explain circular queue. Write algorithms for insertion and deletion in circular queue."
- *2022-23 (Sec A Q1e, 2 Marks): "Differentiate stack and queue."
Solved Exam-Ready Questions
Solved Q1 (2024-25 Sec C Q2b, 7 Marks)
Q: Convert the infix expression A+(BC-(D/E^F)*G)H into postfix expression. Show stack status.
Solution:
Scan left to right:
| Symbol | Stack | Output |
|---|---|---|
| A | A | |
| + | + | A |
| ( | + ( | A |
| B | + ( | AB |
| * | + ( * | AB |
| C | + ( * | ABC |
| - | + ( - | ABC* |
| ( | + ( - ( | ABC* |
| D | + ( - ( | ABC*D |
| / | + ( - ( / | ABC*D |
| E | + ( - ( / | ABC*DE |
| ^ | + ( - ( / ^ | ABC*DE |
| F | + ( - ( / ^ | ABC*DEF |
| ) | + ( - | ABC*DEF^/ |
| * | + ( - * | ABC*DEF^/ |
| G | + ( - * | ABC*DEF^/G |
| ) | + | ABCDEF^/G- |
| * | + * | ABCDEF^/G- |
| H | + * | ABCDEF^/G-H |
| end | ABCDEF^/G-H*+ |
Result: Postfix = ABC*DEF^/G*-H*+.
Solved Q2 (2023-24 Sec C Q2b, 7 Marks)
Q: Explain recursion. Write recursive solution for Tower of Hanoi and compute moves for 4 disks.
Answer:
Recursion solves a problem by calling the same function on a smaller input with a base case. It uses call stack for activation records.
TOH Algorithm: TOH(n,S,D,A): if n==1 move S->D; else TOH(n-1,S,A,D), move S->D, TOH(n-1,A,D,S).
For , moves .
Result: Minimum moves for 4 disks = 15.
Solved Q3 (2024-25 Sec B Q2c, 7 Marks)
Q: Explain circular queue. Write algorithms for insertion and deletion in circular queue.
Answer:
Circular queue connects last position back to first using modulo arithmetic, utilizing freed spaces.
Enqueue: if (rear+1)%MAX==front print overflow; else if front==-1 set front=rear=0; else rear=(rear+1)%MAX; insert item.
Dequeue: if front==-1 underflow; else item=queue[front]; if front==rear set front=rear=-1; else front=(front+1)%MAX; return item.
Result: Circular queue gives enqueue/dequeue and efficient memory reuse.
Practice Questions (Self-Test)
- (2 Marks) Define stack overflow and underflow.
- (2 Marks) What is priority queue? Give two applications.
- (7 Marks) Convert
((A+B)-C*(D/E))+Fto prefix and postfix. Show all steps. - (7 Marks) Evaluate postfix expression
7 8 + 3 2 + /and explain each stack operation. - (7 Marks) Write C functions for push, pop, and display in a linked-list stack. Explain advantage over array stack.
- (7 Marks) Explain deque and circular queue. Compare simple queue, circular queue, and deque with insertion/deletion conditions.