adt stacks and queues. stack: logical level “an ordered group of homogeneous items or elements in...

54
ADT Stacks and Queues

Upload: matthew-holland

Post on 23-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

ADT Stacks and Queues

Page 2: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Logical Level

“An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

A stack is also called a Last In First Out (LIFO) data structure.

Page 3: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Logical Level

Stack Operations:void clear()void push (E it)E pop ()E topValue ()int length();

Page 4: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Application Level• A runtime stack of activation records (ar) is

maintained as a program executes to track function calls and scopes.

• Each activation record contains– space for local variables and parameters– ptr to dynamic parent– ptr to static parent– return address

Page 5: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Consider this codeoutline:

Public class SomeMethods (){

// ------------------------------public static void B ( ){

}

// ------------------------------public static void A ( ){

B ();

}

// ------------------------------public static void main (String[] args ){

A ();

}}

Page 6: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Consider the following:

main begins executingmain calls method Amethod A calls method Bmethod B returns

method A returns

main returns

Push (main’s ar)Push (A’s ar)Push (B’s ar)Use info in Top ar to return control to APop Use info in Top ar to return control to mainPopUse info in Top ar to return control to OSPop

main

A

B

Runtime Stack

Page 7: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Application Level

Stacks can be used to analyze nested expressions with grouping symbols to determine if they are well-formed (all grouping symbols occur in matching pairs and are nested properly.)

( ( {xxx} ) x [ ] xx) is well-formed( ( {xxx} x [ ] ) is ill-formed

Page 8: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

General Algorithmget next symbolset balanced flag to truewhile (there are more input symbols and expression still balanced)

if (next symbol is opening symbol) Push symbol onto stackelse if (next symbol is closing symbol)

if (stack is empty) // check that length is 0 set balanced to falseelse pop the stack to get top opening symbol if (opening symbol does not match closing symbol) set balanced to false

elseignore symbol

get next symbolif (balanced and stack is empty) well-formedelse ill-formed

( ( { x x x } ) x [ ] x x )

(

(

{

[

Stack

{([(Popped

Page 9: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

.

.

.

listArray

[0]

[maxSize - 1]

0

top*

10

maxSize

*Note that top indicates position where next pushed item will go

Page 10: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

70

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

1

top

push ( 70 )

10

maxSize

Page 11: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

7028

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

2

top

push ( 70 )push ( 28)

10

maxSize

Page 12: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702888

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

3

top

push ( 70 )push ( 28)push ( 88)

10

maxSize

Page 13: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702888

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

2

top

push ( 70 )push ( 28)push ( 88)pop ()

10

maxSize

Page 14: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702895

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

3

top

push ( 70 )push ( 28)push ( 88)pop ()push ( 95)

10

maxSize

Page 15: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702895

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

2

top

push ( 70 )push ( 28)push ( 88)pop ()push ( 95)pop ()

10

maxSize

Page 16: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702895

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

1

top

push ( 70 )push ( 28)push ( 88)pop ()push ( 95)pop ()pop ()

10

maxSize

Page 17: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using an array:

702895

.

.

.

listArray

[0]

[MAX_ITEMS - 1]

0

top

push ( 70 )push ( 28)push ( 88)pop ()push ( 95)pop ()pop ()pop ()

10

maxSize

Page 18: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

0

size

Page 19: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

push ( 70 )

70

1

size

Page 20: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

push ( 70 )push ( 28 )

28 70

2

size

Page 21: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

push ( 70 )push ( 28 )push ( 88 )

88 28 70

3

size

Page 22: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL28 70

push ( 70 )push ( 28 )push ( 88 )pop ()

2

size

Page 23: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )

95 28 70

3

Page 24: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL28 70

push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()

2

size

Page 25: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL70

push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()pop ()

1

size

Page 26: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Stack: Implementation Level

Using a linked list:

top

NULL

push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()pop ()pop ()

0

size

Page 27: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Logical Level

“An ordered group of homogeneous items or elements in which items are added at one end (the rear) and removed from the other end (the front.)”

A queue is also called a First In First Out (FIFO) data structure.

Page 28: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Logical Level

Queue Operations:void clear ()void enqueue (E it)E dequeue ()E frontValue ()int length()

Page 29: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Application Level• Perfect for modeling a waiting line in a

simulation program• Key simulation parameters– # of servers– # of queues (waiting lines)– statistics for customer arrival patterns

• Want to minimize customer waiting time• Want to minimize server idle time

Page 30: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Application Level• Queues found all over operating system!– I/O buffers– Job queues waiting for various resources– Spool (print) queue

Page 31: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

0

. . .

rear

front - fixed at [0] (similar to bottom of stack)

*Note that rear indicates position where next enqueued item will go

Page 32: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

1

A . . .

rear

front - fixed at [0] (similar to bottom of stack)

enqueue (A)

Page 33: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

2

A B . . .

rear

front - fixed at [0] (similar to bottom of stack)

enqueue (A)enqueue (B)

Page 34: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

3

A B C . . .

rear

front - fixed at [0] (similar to bottom of stack)

enqueue (A)enqueue (B)enqueue (C)

Page 35: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

3

A B C . . .

rear

front - fixed at [0] (similar to bottom of stack)

enqueue (A)enqueue (B)enqueue (C)dequeue()

But now front is at position[1] , not [0]

Need to shift remaining items down!

Page 36: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 1

items

[0] [maxSize - 1]

2

B C . . .

rear

front - fixed at [0] (similar to bottom of stack)

enqueue (A)enqueue (B)enqueue (C)dequeue()

After the shifting

Is this a very efficient implementation? Θ(n)

Page 37: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

0rear

front 1

Note: Let maxSize = 5for the example

Keep track of both front and rear

rear is actual rear (except when empty)

Note that: length = rear – front + 1

front is actual front (except when empty)

Page 38: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

1

A

rear

front 1

Note: Let maxSize = 5for the example

enqueue (A)

Note that: length = rear – front + 1

Page 39: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

2

A B

rear

front 1

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)

Note that: length = rear – front + 1

Page 40: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

3

A B C

rear

front 1

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)

Note that: length = rear – front + 1

Page 41: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

4

A B C D

rear

front 1

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)enqueue (D)

Note that: length = rear – front + 1

Hmm . . . Queue now appears full . . . but

Page 42: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

4

A B C D

rear

front 2

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)enqueue (D)dequeue ()

Note that: length = rear – front + 1

Page 43: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

4

A B C D

rear

front 3

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)enqueue (D)dequeue ()dequeue ()

Note that: length = rear – front + 1

What if we want to Enqueue a couple of more items?

Why not let Queue elements “wrap around” in array?

Page 44: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

0

E A B C D

rear

front 3

Note: Let maxSize = 5for the example

Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue ()Dequeue ()Enqueue (E)

Note that: length = rear – front + 1 ???

Note: to advance the rear indicator :

rear = (rear + 1) % maxSize

correction: length = ((rear+maxSize) – front + 1) % maxSize

Page 45: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

1

E F B C D

rear

front 3

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)enqueue (D)dequeue ()dequeue ()enqueue (E)enqueue (F)

remember: length = ((rear+maxSize) – front + 1) % maxSize

Page 46: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

2

E F G C D

rear

front 3

Note: Let maxSize = 5for the example

enqueue (A)enqueue (B)enqueue (C)enqueue (D)dequeue ()dequeue ()enqueue (E)enqueue (F)enqueue (G)

remember: length = ((rear+maxSize) – front + 1) % maxSize

Page 47: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation Level

Using an array: Option 2

items

[0] [4]

2

E F G C D

rear

front 3

Note: Let maxSize = 5for the example

Also Note: to advance the rear or front indicators:

rear = (rear + 1) % maxSizefront = (front+1) % maxSize

Now queue REALLY IS full!

But look at values of front and rear and . . .

Oops! This is supposed to mean queue is empty !!! ???

remember: length = ((rear+maxSize) – front + 1) % maxSize

length = (( 2 + 5) – 3 + 1) % 5 = 0

Solution: Don’t let this happen; “waste” an array position!

Page 48: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear

0size

Page 49: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear enqueue (A)

A

1size

Page 50: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear enqueue (A)enqueue (B)

A B

2size

Page 51: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear enqueue (A)enqueue (B)enqueue (C)

A B C

3size

Page 52: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear enqueue (A)enqueue (B)enqueue (C)dequeue ()

B C

2size

Page 53: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear enqueue (A)enqueue (B)enqueue (C)dequeue ()dequeue ()

C

0size

Page 54: ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”

Queue: Implementation LevelUsing a linked list:

front

NULL

rear

enqueue (A)enqueue (B)enqueue (C)dequeue ()dequeue ()dequeue ()0size