Stacks, Queues (Chapter - 6)
Stacks, Queues (Chapter - 6)
2
What is Stack?
A Stack: Stores a set of elements in a
particular order
Analogy
A stack of coins
A stack of dishes in a cafeteria
(b)
E top
D top D D top
C top C C C
B top B B B B
A top A A A A A
4
Stack Operations
Stack Operations
Create an empty stack
Destroy a stack
Determine whether a stack is empty
Add a new item
Remove the item that was added most recently
Retrieve the item that was added most recently
5
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer
science
Program execution stack
Evaluating expressions
6
Array-Based Stack Implementation
If top=0 then
print ‘STACK UNDERFLOW’
exit
else
item=a[top]
end if
top = top-1
Exit
8
Array-Based Stack Implementation
Display(top,i,a[i]):
If top=0 then
Print ‘STACK EMPTY’
exit
else
for i= top to 0
print a[i]
end for
exit
9
Algebraic Expressions
Infix Expressions
An operator appears between its operands
Example: a + b Expressions
a+b
Prefix Expressions
An operator appears before its operands
Example: + a b Operand Operator
Postfix Expressions
An operator appears after its operands
Example: a b +
10
Algebric Expressions
Postfix Expressions(operand|operand|operator)
ab *c+
11
Level of Precedence
Highest
Next Highest *, /
Lowest +, -
12
Evaluation of a Postfix Expressions
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
6 +2 = 8
(4) + 5, 8
5 * 8 = 40
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
12 / 4 = 3
(8) / 40, 3
40 - 3 = 37
(9) - 37
(10) ) 14
Transforming Infix into Postfix Expressions
Algorithm: POLISH(Q, P)
1. Push “(” onto STACK, and add “)” to the end of Q.
2. Scan Q from left to right and repeat steps 3 to 6 for
each element of Q until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis “(” is encountered, push it onto
STACK.
5. If an operator × is encountered, then:
a) Repeatedly pop from STACK and add to P each
operator (on the top of STACK) which has the same
precedence as or higher precedence than × .
b) Add × to STACK.
15
Continued
Algorithm: POLISH(Q, P)
6. If a right parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each
operator (on the top of STACK) until a left
parenthesis is encountered.
b) Remove the left parenthesis. [Do not add the left
parenthesis to P.]
7. Exit
16
Example (Infix to Postfix)
Infix Expression Q: A + ( B * C – ( D / E ↑ F ) * G ) * H
(1) A ( A
(2) + (+ A
(3) ( (+( A
(4) B (+( AB
(5) * (+(* AB
(6) C (+(* ABC
(7) - (+(- ABC *
(8) ( (+(-( ABC *
(9) D (+(-( ABC * D
(10) / (+(-(/ ABC * D 17
Example (Infix to Postfix)
Infix Expression Q: A + ( B * C – ( D / E ↑ F ) * G ) * H
19
Factorial Function
20
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
21
Factorial Recursive Definition in C
int fact(int n)
{
if (n == 0)
return (1);
else
return (n * fact(n-1));
}
** Self
Write the algorithm for calculating factorial of a number
using Recursion or Iteration.
22
Fibonacci Sequence
** Self
Write the algorithm for finding the Fibonacci number
using Recursion or Iteration.
23
The Towers of Hanoi
Given
✓ three poles
✓ a set of discs on the first pole, discs of
different sizes, the smallest discs at
the top
Goal
✓ move all the discs from the left pole to
the right one.
Condition
✓ only one disc may be moved at a time. A B C
✓ A disc can be placed either on an
empty pole or on top of a larger disc.
24
The Towers of Hanoi
A B C
25
The Towers of Hanoi
A B C
26
The Towers of Hanoi
A B C
27
The Towers of Hanoi
A B C
28
The Towers of Hanoi
A B C
29
The Towers of Hanoi
A B C
30
The Towers of Hanoi
A B C
31
The Towers of Hanoi
A B C
32
Solution for Single Disc
1 1
33
Solution for 2 Discs
2 2
1 2 1
34
Solution for 3 Discs
3 3
2 3 2
1 2 1
35
Solution for n Discs
3
2
1
36
Solution for n Discs
Program
1) TOWER (n-1, A, C, B)
2) Printf (Move a disc from A to C)
3) TOWER (n-1, B, A, C)
37
Tracing for 3 Discs
Program
1) TOWER (n-1, Source, Dest, AUX)
2) Printf (Move a disc from Source to Dest) 1) A → Source
3) TOWER (n-1, AUX, Source, Dest) 2) B → AUX
3) C → Dest
1) A → Source
TOWER (3, A, B, C) 1) B → Source
2) C → AUX
3) B → Dest 2) A → AUX
3) C → Dest
A→C
TOWER (2, A, C, B) TOWER (2, B, A, C)
4
A→B B→C
A→C 1 3 C→B
5 B→A 7 A→C
38
Solution for 3 Discs
3 3
2 3 2
13 2 13
39
Solution for 4 Discs
**Self:
40
What is Queue?
A Queue: is a linear list of elements in which deletions can
take place only at one end, called FRONT, and
Insertions can take place only at other end, called the
REAR.
Analogy
Automobiles waiting
People waiting in a bank
Timesharing system in CS
41
Array Representation of Queue
FRONT REAR
FRONT REAR
FRONT REAR
42
Array Representation of Queue
FRONT
.......... DDD
1 2 3 4 5 6 7 8 N
REAR
43
Circular Queue
Initially Empty
1 2 3 4 5
Insert (A, B, C ) A B C
1 2 3 4 5
FRONT REAR
Delete ( A ) B C
1 2 3 4 5
FRONT REAR
Insert ( D, E ) B C D E
1 2 3 4 5
FRONT REAR 44
Circular Queue
Delete ( B, C ) D E
1 2 3 4 5
FRONT REAR
Insert ( F ) F D E
1 2 3 4 5
REAR FRONT
Delete ( D ) F E
1 2 3 4 5
REAR FRONT
Insert ( G, H ) F G H E
1 2 3 4 5
REAR FRONT 45
Circular Queue
Delete ( E ) F G H
1 2 3 4 5
FRONT REAR
Delete ( F ) G H
1 2 3 4 5
FRONT REAR
Insert ( K ) G H K
1 2 3 4 5
FRONT REAR
Delete ( G, H ) K
1 2 3 4 5
REAR FRONT 46
Algorithm 1 (Enqueue)
47
Algorithm 2 (Dequeue)
48
Priority Queue
49
One Way List Representation
Each node in the list will contain three items of
information: an information field Info, a priority
number PRN and a link number LINK.
50
One Way List Representation
AAA 1 BBB 2 CCC 2 DDD 4
START
1 BBB 2 6
2 7
3 DDD 4 4
4 EEE 4 9
5 AAA 1 1
6 CCC 2 3
7 10 One Way List Representation
8 GGG 5 0 of Queue
9 FFF 4 8
10 11
11 12
12 0
51
Adding Element in PQ
52
Adding Elements in PQ
ZZZ 7
START
NNN 4
53
Thank You
54