Click here to load reader
Upload
sanjay-saha
View
151
Download
1
Embed Size (px)
DESCRIPTION
Very basic introductory (5 mins) presentation on Stack and queue (data structure).
Citation preview
STACK, QUEUESANJAY 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
BA
DCBA
CBA
DCBA
EDCBA
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
ABA
CBA
DCBA
DCBrear
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 queueSanjay Saha
University of Dhaka
Thank you!