Top Banner Ad

BCS301 — Unit 2: Stacks and Queues

Table of Contents (23 sections)

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:

  1. Infix to postfix: Use operator stack; precedence (^ highest, then */ , then +-) and associativity (^ right-to-left). Show stack, input symbol, and output at every step.
  2. Postfix evaluation: Scan left to right; push operands, on operator pop two operands (second popped is left operand), apply operator, push result.
  3. Circular queue: Use (rear+1)%size==front for full and front==-1 for empty; after dequeue of last element reset front=rear=-1.
  4. Recursion: Write base case, recursive case, and stack unwinding for factorial/Tower of Hanoi; TOH moves = 2n−12^n-1.
  5. 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 O(1)O(1)
pop() Remove and return top Underflow if top==-1 O(1)O(1)
peek()/top() Return top without removing Underflow if empty O(1)O(1)
isEmpty() Check if stack empty top==-1 O(1)O(1)
isFull() Check if array stack full top==MAX-1 O(1)O(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 (A+B∗CA+B*C) Parentheses Needed
Infix operator between operands A+B∗CA+B*C Yes
Prefix (Polish) operator before operands +A∗BC+A*BC No
Postfix (Reverse Polish) operator after operands ABC∗+ABC*+ No

2.2 Infix to Postfix (Stack Algorithm)

  1. Scan infix left to right.
  2. Operand: append to output.
  3. (: push to stack.
  4. ): pop to output until (.
  5. 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.
  6. After scan, pop remaining operators.

Precedence: ^ = 3 (right associative), * / = 2, + - = 1.

Worked Example: Convert A + B * C ^ D - E to postfix.

A→A,  +→stack,  B→AB,  ∗→stack(+,∗),  C→ABC,  ^→stack(+,∗,^),  D→ABCDA \to A,\; +\to stack,\; B\to AB,\; *\to stack(+,*),\; C\to ABC,\; \hat{}\to stack(+,*,\hat{}),\; D\to ABCD −→pop ^,∗,+→ABCD^∗+,  push −,  E→ABCD^∗+E,  pop −-\to \text{pop }\hat{},*,+ \to ABCD\hat{}*+,\; \text{push }-,\; E\to ABCD\hat{}*+E,\; \text{pop }- Postfix: ABCD^∗+E−\text{Postfix: }ABCD\hat{}*+E-

2.3 Postfix Evaluation

Scan left to right: push operands; on operator, pop op2op2, pop op1op1, compute op1  op  op2op1\;op\;op2, push result. Final stack value is answer.

Worked Example: Evaluate postfix 2 3 1 * + 9 -.

2,3,1→push;  ∗→3∗1=3;  +→2+3=5;  9→push;  −→5−9=−42,3,1\to push;\; *\to 3*1=3;\; +\to 2+3=5;\; 9\to push;\; -\to 5-9=-4

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 nn disks from source to destination using auxiliary peg; only one disk moved at a time; larger disk cannot be placed on smaller.

Recursive solution:

  1. Move n−1n-1 disks from Source to Auxiliary.
  2. Move nnth disk from Source to Destination.
  3. Move n−1n-1 disks from Auxiliary to Destination.
T(n)=2T(n−1)+1,  T(1)=1⇒T(n)=2n−1T(n)=2T(n-1)+1,\;T(1)=1 \quad \Rightarrow \quad T(n)=2^n-1

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-1 overflow; else rear++; queue[rear]=x.
  • Dequeue: if front==-1 || front>rear underflow; else x=queue[front++].
  • Drawback: unused space at beginning after dequeues.

4.2 Circular Queue

Overcomes simple queue space waste by wrapping around.

Circular Queue Conditions:

Full: (rear+1)%MAX==front\text{Full: }(rear+1)\%MAX == front Empty: front==−1\text{Empty: }front == -1 Enqueue rear: rear=(rear+1)%MAX,  Dequeue front: front=(front+1)%MAX\text{Enqueue rear: }rear=(rear+1)\%MAX,\; \text{Dequeue front: }front=(front+1)\%MAX

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 n=4n=4, moves =24−1=15=2^4-1=15.

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 O(1)O(1) enqueue/dequeue and efficient memory reuse.


Practice Questions (Self-Test)

  1. (2 Marks) Define stack overflow and underflow.
  2. (2 Marks) What is priority queue? Give two applications.
  3. (7 Marks) Convert ((A+B)-C*(D/E))+F to prefix and postfix. Show all steps.
  4. (7 Marks) Evaluate postfix expression 7 8 + 3 2 + / and explain each stack operation.
  5. (7 Marks) Write C functions for push, pop, and display in a linked-list stack. Explain advantage over array stack.
  6. (7 Marks) Explain deque and circular queue. Compare simple queue, circular queue, and deque with insertion/deletion conditions.