stack and queue - university of marylandqueue • queue is an adt data structure similar to stack,...

42
Stack and Queue 6/19/16 1 CMSC 132, Summer 2016 Object-Oriented Programming II Anwar Mamat

Upload: others

Post on 29-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackandQueue

6/19/16 1

CMSC132,Summer2016Object-OrientedProgrammingII

AnwarMamat

Page 2: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Stack

• Allowsaccesstoonlythelastiteminserted.

• Anitemisinsertedorremovedfromthestackfromoneendcalledthe“top” ofthestack.

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

6/19/16 2

Page 3: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• EmptyStack

6/19/16 3

Page 4: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• Stack

Push10

10Top

6/19/16 4

Page 5: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• Stack

Push10

10

Top

Push20

20

Push30

30

6/19/16 5

Page 6: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• Stack

Push10

10Top

Push20

20

Push30

Pop

6/19/16 6

Page 7: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• Stack

Push10

10

Top

Push20

20

Push30

PopPush10

10

6/19/16 7

Page 8: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Applications

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

6/19/16 8

Page 9: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackInterface

6/19/16 9

Page 10: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

ImplementStackUsinganArray

6/19/16 10

Page 11: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• EmptyStack

N

4

3

2

1

0

6/19/16 11

Page 12: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Pushinganitem

N

4

3

2

1

0

Push10

10

6/19/16 12

Page 13: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Pushingmoreitems

N

4

3

2

1

0 10

Push10

Push20

Push30

20

30

Whatifthearrayisfull?

6/19/16 13

Page 14: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Removinganitem

N

4

3

2

1

0 10

Push10

Push20

Push30

20Pop

6/19/16 14

Page 15: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Removinganitem

N

4

3

2

1

0 10

Push10

Push20

Push30

20Pop

Pop

6/19/16 15

Page 16: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

ArrayImplementation:push

6/19/16 16

N

4

3

2

1

0 10

Page 17: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

ArrayImplementation:pop

Why?

6/19/16 17

Page 18: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

ArrayImplementation:peek

6/19/16 18

Page 19: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

ImplementStackUsingaLinkedList

6/19/16 19

Page 20: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

StackExample

• EmptyStack

first=nullN=0

6/19/16 20

Page 21: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Pushinganitem

firstN=1

10

Push10

6/19/16 21

Page 22: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Pushingmoreitems

firstN=3

30

Push10

Push20

Push30

1020

6/19/16 22

Page 23: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Pushingmoreitems

firstN=2

20

Push10

Push20

Push30

10

Pop

6/19/16 23

Page 24: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

OthermethodsPeek

size

isEmpty

6/19/16 24

Page 25: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

TestingtheStack

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

Page 26: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Queue• QueueisanADTdatastructuresimilartostack,exceptthatthefirstitemtobeinsertedisthefirstonetoberemoved.

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

• Removinganitemfromaqueueiscalled“deletionordequeue”,whichisdoneattheotherendofthequeuecalled“front”.

• Applications:printerqueue,keystrokequeue,etc.

6/19/16 26

Page 27: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Queue

6/19/16 27

Page 28: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

QueueExampleEmptyQueue

6/19/16 28

Page 29: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

AddinganitemEmptyQueue

enqueue 10

10

6/19/16 29

Page 30: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

AddingmoreitemsEmptyQueue

enqueue 10

30

enqueue 20

20

enqueue 30

10

6/19/16 30

Page 31: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

AddingmoreitemsEmptyQueue

enqueue 10

30

enqueue 20

20

enqueue 30dequeue

6/19/16 31

Page 32: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

QueueInterface

6/19/16 32

Page 33: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 33

012345

first last

N=0

Page 34: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 34

10 20 30

012345

first last

enqueue 10

enqueue 20enqueue 30

N=3

Page 35: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 35

20 30

012345

first last

enqueue 10

enqueue 20enqueue 30

dequeueN=2

Page 36: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 36

20 30 40 50

012345

first last

enqueue 10

enqueue 20enqueue 30

dequeueN=4

enqueue 40enqueue 50

Page 37: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 37

20 30 40 50 60

012345

firstlast

enqueue 10

enqueue 20enqueue 30

dequeueN=5

enqueue 40enqueue 50enqueue 60

Page 38: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

CircularArrayImplementation

6/19/16 38

30 40 50 60

012345

firstlast

enqueue 10

enqueue 20enqueue 30

dequeueN=4

enqueue 40enqueue 50enqueue 60dequeue

Page 39: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Implementingpush(enqueue)

6/19/16 39

30 40 50 60

012345

firstlast

Page 40: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Implementingpop(dequeue)

6/19/16 40

30 40 50 60

012345

firstlast

Page 41: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

Implementingresize

6/19/16 41

70 30 40 50 60

012345

firstlast

30 40 50 60 70

first last

Page 42: Stack and Queue - University Of MarylandQueue • Queue is an ADT data structure similar to stack, except that the first item to be inserted is the first one to be removed. • This

6/19/16 42

ImplementQueueUsingaLinkedList

firstN=3

10 3020

last

• Addanewitem• Newnodeisadded totail(last.next)

• Removeaitem• Removethefirstnode (first=first.next)