Data Structures and Algorithms · Unit 3
Linear Data Structures: Stacks and Queues
Exam-focused notes for Linear Data Structures: Stacks and Queues (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.
Linear Data Structures: Stacks and Queues in short
Stacks and queues are linear structures that restrict where we add and remove items. They are used in expression evaluation, function calls, scheduling and many other tasks.
What this unit covers
- Stack definition and operations
- Queue definition and operations
- Differentiation between stack and queue
- Simple queue implementation
- Circular queue advantages and implementation
- Priority queue definition and implementation
- Queue implementation using linked list
- Stack implementation using linked list
- Infix to postfix conversion using stack
- Postfix expression evaluation algorithm
Circular queue advantages and implementation
2080 · 5 marks
What is circular queue? How can you implement circular queue? [5]
0 · 5 marks
Why circular queue is advantageous over linear queue? Write algorithm for enqueue and is full operation for circular queue. [5]
Infix to postfix conversion using stack
2082 · 5 marks
Convert the infix expression 7 * 8 + 10 - 2 to postfix using stack. [5]
Show solved answerHide answer
Given Data
Infix expression to convert: $$7 * 8 + 10 - 2$$
Operator precedence:
*,/: higher+,-: lower
Associativity: left to right.
Conversion Rules (Stack Method)
- Scan tokens left to right.
- Operand → append to output.
- Operator → pop operators from stack with precedence greater than or equal to the current one (for left-associative operators), append them to output, then push the current operator.
- End → pop all remaining operators to output.
Step-by-Step Trace
| Step | Token | Action | Stack (bottom→top) | Output |
|---|---|---|---|---|
| 1 | 7 | operand → output | (empty) | 7 |
| 2 | * | push | * | 7 |
| 3 | 8 | operand → output | * | 7 8 |
| 4 | + | * ≥ + so pop *; push + | + | 7 8 * |
| 5 | 10 | operand → output | + | 7 8 * 10 |
| 6 | - | + ≥ - so pop +; push - | - | 7 8 * 10 + |
| 7 | 2 | operand → output | - | 7 8 * 10 + 2 |
| 8 | End | pop - | (empty) | 7 8 * 10 + 2 - |
Verification (evaluate postfix)
Given values: $7,8,+,*$ etc. Compute $7 \times 8 = 56$; $56 + 10 = 66$; $66 - 2 = 64$. Infix: $7 \times 8 + 10 - 2 = 56 + 10 - 2 = 64$. ✓ Matches.
Result
$$\boxed{7\ 8\ *\ 10\ +\ 2\ -}$$
2078 · 10 marks
Explain algorithm to convert an infix expression to postfix using stack? Use this algorithm to convert (A+B)*C-D to postfix.[10]
Simple queue implementation
2082 · 5 marks
What is simple queue? Describe about any three types of graphs. [2+3]
Show solved answerHide answer
Simple Queue and Types of Graphs
Simple Queue [2 marks]
A simple queue (also called a linear queue) is a linear data structure that follows the FIFO (First In First Out) principle, meaning the element inserted first is the element removed first.
Key characteristics:
- Elements are inserted from the REAR end
- Elements are deleted from the FRONT end
- Once the rear reaches the maximum size, no more insertions are possible (even if space exists at the front after deletions)
FRONT REAR
| |
v v
[ 10 | 20 | 30 | 40 | 50 ]
^ ^
Delete Insert
Basic Operations:
- Enqueue: Insert an element at the rear
- Dequeue: Remove an element from the front
- Peek/Front: View the front element without removing it
Three Types of Graphs [3 marks]
A graph is a non-linear data structure consisting of a set of vertices (nodes) and edges (connections) represented as G = (V, E).
1. Directed Graph (Digraph)
A graph in which every edge has a specific direction is called a directed graph. The edges are represented as ordered pairs (u, v), meaning the edge goes from vertex u to vertex v but not necessarily from v to u.
A -----> B
| |
v v
C -----> D
- Edge (A, B) means A points to B
- Used in: web page linking, task scheduling
2. Undirected Graph
A graph in which edges have no direction is called an undirected graph. The edges are represented as unordered pairs {u, v}, meaning the connection is bidirectional.
A ----- B
| |
| |
C ----- D
- Edge {A, B} means both A connects to B and B connects to A
- Used in: social networks, road maps
3. Weighted Graph
A graph in which each edge is assigned a numerical value (weight or cost) is called a weighted graph. It can be either directed or undirected.
A ---5--- B
| |
3 7
| |
C ---2--- D
- The weight may represent distance, cost, time, or capacity
- Used in: shortest path algorithms (Dijkstra's), network routing, GPS navigation
Note: Other types include cyclic/acyclic graphs, connected/disconnected graphs, and complete graphs.
Postfix expression evaluation algorithm
2080 · 10 marks
What is stack? Explain different stack operations. Explain algorithm to evaluate postfix expression.[10]
Show solved answerHide answer
Stack: Definition, Operations, and Postfix Evaluation
1. What is a Stack?
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means the element inserted last is the first one to be removed.
A stack can be visualized as a pile of plates: you add a plate on top and also remove from the top.
Key characteristics:
- All insertions and deletions occur at one end called the TOP
- It is an ordered collection of elements
- It can be implemented using arrays or linked lists
2. Stack Operations
The following are the primary operations performed on a stack:
a) PUSH (Insertion)
Adds an element to the top of the stack.
Algorithm: PUSH(STACK, TOP, MAXSIZE, ITEM)
1. If TOP = MAXSIZE - 1, then:
Print "Stack Overflow" and EXIT
2. Set TOP = TOP + 1
3. Set STACK[TOP] = ITEM
4. Return
b) POP (Deletion)
Removes and returns the top element from the stack.
Algorithm: POP(STACK, TOP, ITEM)
1. If TOP = -1, then:
Print "Stack Underflow" and EXIT
2. Set ITEM = STACK[TOP]
3. Set TOP = TOP - 1
4. Return ITEM
c) PEEK / TOP
Returns the top element without removing it.
1. If TOP = -1, then:
Print "Stack is Empty" and EXIT
2. Return STACK[TOP]
d) isEmpty
Checks whether the stack is empty.
1. If TOP = -1, return TRUE
2. Else return FALSE
e) isFull
Checks whether the stack is full.
1. If TOP = MAXSIZE - 1, return TRUE
2. Else return FALSE
3. Postfix Expression Evaluation
What is a Postfix Expression?
In postfix notation (also called Reverse Polish Notation), the operator comes after its operands.
- Infix:
A + B - Postfix:
A B +
Postfix expressions do not require parentheses and are easy to evaluate using a stack.
Algorithm to Evaluate Postfix Expression
Input: A postfix expression string
Output: The evaluated result
Algorithm EVALUATE_POSTFIX(expression):
1. Create an empty stack S
2. Scan the expression from LEFT to RIGHT, one token at a time
3. For each token:
a. If token is an OPERAND (number):
PUSH it onto stack S
b. If token is an OPERATOR (+, -, *, /):
i. POP the top element, call it OPERAND2
ii. POP the next top element, call it OPERAND1
iii.Apply the operator: RESULT = OPERAND1 operator OPERAND2
iv. PUSH RESULT onto stack S
4. After scanning all tokens:
POP and return the final value from stack S
(This is the result of the expression)
Note: The order matters: first popped value is the right operand, second popped value is the left operand.
Worked Example
Evaluate postfix expression: 5 3 + 2 * 4 -
| Step | Token | Action | Stack State |
|---|---|---|---|
| 1 | 5 | PUSH 5 | [5] |
| 2 | 3 | PUSH 3 | [5, 3] |
| 3 | + | POP 3 and 5, compute 5+3=8, PUSH 8 | [8] |
| 4 | 2 | PUSH 2 | [8, 2] |
| 5 | * | POP 2 and 8, compute 8*2=16, PUSH 16 | [16] |
| 6 | 4 | PUSH 4 | [16, 4] |
| 7 | - | POP 4 and 16, compute 16-4=12, PUSH 12 | [12] |
Final Result = 12
Another Example
Evaluate: 6 2 3 + - 3 8 2 / + *
| Token | Action | Stack |
|---|---|---|
| 6 | PUSH | [6] |
| 2 | PUSH | [6, 2] |
| 3 | PUSH | [6, 2, 3] |
| + | POP 3,2 → 2+3=5, PUSH | [6, 5] |
| - | POP 5,6 → 6-5=1, PUSH | [1] |
| 3 | PUSH | [1, 3] |
| 8 | PUSH | [1, 3, 8] |
| 2 | PUSH | [1, 3, 8, 2] |
| / | POP 2,8 → 8/2=4, PUSH | [1, 3, 4] |
| + | POP 4,3 → 3+4=7, PUSH | [1, 7] |
| * | POP 7,1 → 1*7=7, PUSH | [7] |
Final Result = 7
Summary
| Operation | Description | Time Complexity |
|---|---|---|
| PUSH | Insert element at top | O(1) |
| POP | Remove element from top | O(1) |
| PEEK | View top element | O(1) |
| isEmpty | Check if stack is empty | O(1) |
| isFull | Check if stack is full | O(1) |
| Postfix Evaluation | Scan and compute | O(n) |
Every basic stack operation touches only the top of the stack, so each of them runs in constant time. Postfix evaluation scans the expression once and performs a constant amount of work per token, so it is O(n) in time and O(n) in space for the stack in the worst case, where n is the number of tokens.
Conclusion
A stack is a linear list restricted to LIFO access through PUSH and POP at a single end, with PEEK, isEmpty and isFull as supporting operations. Its usefulness is best seen in expression evaluation: because postfix notation carries no parentheses and no precedence, a single left to right scan with one stack is enough, pushing every operand and replacing the top two operands by the result whenever an operator appears. The value left on the stack at the end of the scan is the value of the expression.
Queue implementation using linked list
2080 · 5 marks
How can we use linked list to implement queue? Explain. [5]
2078 · 5 marks
How can you implement queue using linked list? Explain. [5]
Priority queue definition and implementation
2079 · 5 marks
Explain priority queue with example. What is circular queue? [5]
2078 · 5 marks
Define priority queue. How do you implement priority queue? Explain. [5]
Differentiation between stack and queue
0 · 10 marks
Differentiate stack with queue? Trace an algorithm for converting infix expression to postfix for the following infix expression: (A+B)*(C$(D-E)+F)-G[10]
Make Unit 3 stick
Practice BIT201 with flashcards & quizzes