9

Click here to load reader

Stack and Queue (brief)

Embed Size (px)

DESCRIPTION

Very basic introductory (5 mins) presentation on Stack and queue (data structure).

Citation preview

Page 1: Stack and Queue (brief)

STACK, QUEUESANJAY SAHA

Page 2: Stack and Queue (brief)

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

Page 3: Stack and Queue (brief)

STACK: VISUAL SIMULATION

BA

DCBA

CBA

DCBA

EDCBA

top

top

top

top

top

A

Input : A, B, C, D, E

popped

Page 4: Stack and Queue (brief)

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

Page 5: Stack and Queue (brief)

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

Page 6: Stack and Queue (brief)

ABA

CBA

DCBA

DCBrear

front

rear

front

rear

front

rear

front

rear

front

QUEUE: VISUAL SIMULATION

Input : A, B, C, D, E

Page 7: Stack and Queue (brief)

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

Page 8: Stack and Queue (brief)

SUMMARY

Page 9: Stack and Queue (brief)

Stack and queueSanjay Saha

University of Dhaka

Thank you!