KEMBAR78
Stack and Queue (brief) | PDF
STACK, QUEUE
SANJAY SAHA
STACK: WHAT & WHY
 Collection of elements of same type
 LAST IN FIRST OUT = LIFO
 The last elements that is put in it, is the first one to get out of it!
 Real life examples:
 Plate stack
 Book shelf
 In computer science studies:
 Recursion, function calls
 Infix to prefix, postfix evaluation
 Operating systems
STACK: VISUAL SIMULATION
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
A
Input : A, B, C, D, E
popped
STACK: EXAMPLE
 Factorial
 6! = 6 x 5! = 6 x 5 x 4! = … = 6 x 5 x 4 x 3 x 2 x 1
Recursive solution
int fac(int numb){
if(numb<=1)
return 1;
else
return numb*fac(numb-1);
}
fac(1)
fac(2)
fac(3)
fac(4)
fac(5)
fac(6)
2 x 1
3 x 2 x 1
4 x 3 x 2 x 1
5 x 4 x 3 x 2 x 1
6 x 5 x 4 x 3 x 2 x 1
QUEUE: WHAT & WHY
 FIRST IN FIRST OUT = FIFO
 The first elements that is put in it, will be the first one to get served!
 Real life examples:
 Ticket queue
 Restaurant
 In computer science studies:
 Threads
 CPU job scheduling
 Sorting
A
B
A
C
B
A
D
C
B
A
D
C
Brear
front
rear
front
rear
front
rear
front
rear
front
QUEUE: VISUAL SIMULATION
Input : A, B, C, D, E
QUEUE: EXAMPLE
 Circular Queues
 The rear of the queue can be behind the
front
 Effective time utilization
b c d
FrontRear
b c d
FrontRear
e f
e fg
SUMMARY
Stack and queue
Sanjay Saha
University of Dhaka
Thank you!

Stack and Queue (brief)