Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack,...

Preview:

Citation preview

StackandQueue

6/19/16 1

CMSC132,Summer2016Object-OrientedProgrammingII

AnwarMamat

Stack

• Allowsaccesstoonlythelastiteminserted.

• Anitemisinsertedorremovedfromthestackfromoneendcalledthe“top” ofthestack.

• ThismechanismiscalledLast-In-First-Out(LIFO).

6/19/16 2

StackExample

• EmptyStack

6/19/16 3

StackExample

• Stack

Push10

10Top

6/19/16 4

StackExample

• Stack

Push10

10

Top

Push20

20

Push30

30

6/19/16 5

StackExample

• Stack

Push10

10Top

Push20

20

Push30

Pop

6/19/16 6

StackExample

• Stack

Push10

10

Top

Push20

20

Push30

PopPush10

10

6/19/16 7

Applications

• JVMstackmachine• FunctionCall• ExpressionEvaluation• PushdownAutomata• Recursiveà IterativeFunctionConversion

6/19/16 8

StackInterface

6/19/16 9

ImplementStackUsinganArray

6/19/16 10

StackExample

• EmptyStack

N

4

3

2

1

0

6/19/16 11

Pushinganitem

N

4

3

2

1

0

Push10

10

6/19/16 12

Pushingmoreitems

N

4

3

2

1

0 10

Push10

Push20

Push30

20

30

Whatifthearrayisfull?

6/19/16 13

Removinganitem

N

4

3

2

1

0 10

Push10

Push20

Push30

20Pop

6/19/16 14

Removinganitem

N

4

3

2

1

0 10

Push10

Push20

Push30

20Pop

Pop

6/19/16 15

ArrayImplementation:push

6/19/16 16

N

4

3

2

1

0 10

ArrayImplementation:pop

Why?

6/19/16 17

ArrayImplementation:peek

6/19/16 18

ImplementStackUsingaLinkedList

6/19/16 19

StackExample

• EmptyStack

first=nullN=0

6/19/16 20

Pushinganitem

firstN=1

10

Push10

6/19/16 21

Pushingmoreitems

firstN=3

30

Push10

Push20

Push30

1020

6/19/16 22

Pushingmoreitems

firstN=2

20

Push10

Push20

Push30

10

Pop

6/19/16 23

OthermethodsPeek

size

isEmpty

6/19/16 24

TestingtheStack

Size:77,7,6,6,5,5,4,4,3,3,2,2,1,16/19/16 25

Queue• QueueisanADTdatastructuresimilartostack,exceptthatthefirstitemtobeinsertedisthefirstonetoberemoved.

• ThismechanismiscalledFirst-In-First-Out(FIFO).• Placinganiteminaqueueiscalled“insertionorenqueue”,whichisdoneattheendofthequeuecalled“rear”.

• Removinganitemfromaqueueiscalled“deletionordequeue”,whichisdoneattheotherendofthequeuecalled“front”.

• Applications:printerqueue,keystrokequeue,etc.

6/19/16 26

Queue

6/19/16 27

QueueExampleEmptyQueue

6/19/16 28

AddinganitemEmptyQueue

enqueue 10

10

6/19/16 29

AddingmoreitemsEmptyQueue

enqueue 10

30

enqueue 20

20

enqueue 30

10

6/19/16 30

AddingmoreitemsEmptyQueue

enqueue 10

30

enqueue 20

20

enqueue 30dequeue

6/19/16 31

QueueInterface

6/19/16 32

CircularArrayImplementation

6/19/16 33

012345

first last

N=0

CircularArrayImplementation

6/19/16 34

10 20 30

012345

first last

enqueue 10

enqueue 20enqueue 30

N=3

CircularArrayImplementation

6/19/16 35

20 30

012345

first last

enqueue 10

enqueue 20enqueue 30

dequeueN=2

CircularArrayImplementation

6/19/16 36

20 30 40 50

012345

first last

enqueue 10

enqueue 20enqueue 30

dequeueN=4

enqueue 40enqueue 50

CircularArrayImplementation

6/19/16 37

20 30 40 50 60

012345

firstlast

enqueue 10

enqueue 20enqueue 30

dequeueN=5

enqueue 40enqueue 50enqueue 60

CircularArrayImplementation

6/19/16 38

30 40 50 60

012345

firstlast

enqueue 10

enqueue 20enqueue 30

dequeueN=4

enqueue 40enqueue 50enqueue 60dequeue

Implementingpush(enqueue)

6/19/16 39

30 40 50 60

012345

firstlast

Implementingpop(dequeue)

6/19/16 40

30 40 50 60

012345

firstlast

Implementingresize

6/19/16 41

70 30 40 50 60

012345

firstlast

30 40 50 60 70

first last

6/19/16 42

ImplementQueueUsingaLinkedList

firstN=3

10 3020

last

• Addanewitem• Newnodeisadded totail(last.next)

• Removeaitem• Removethefirstnode (first=first.next)