You are on page 1of 62

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. A stack is also called a Last In First Out (LIFO) data structure.

Stack: Logical Level


Stack Operations: Boolean IsEmpty () Boolean IsFull () Push (ItemType newitem) void Pop () ItemType Top ()

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

// ------------------------------

Consider this code outline:

void B ( ) { } // -----------------------------void A ( ) { B (); } // -----------------------------int main ( ) { A (); return 0; }

Consider the following:


main begins executing main calls function A function A calls function B function B returns function A returns main returns

B
A

main

Push (mains ar) Push (As ar) Runtime Stack Push (Bs ar) Use info in Top ar to return control to A Pop Use info in Top ar to return control to main Pop Use info in Top ar to return control to OS Pop

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

General Algorithm ( ( { x x x } ) x [ ] x x )
get next symbol set balanced flag to true while (there are more input symbols and expression still balanced) if (next symbol is opening symbol) Push symbol onto stack else if (next symbol is closing symbol) if (stack is empty) set balanced to false else use Top to get copy of opening symbol on top of stack Pop the stack if (opening symbol does not match closing symbol) set balanced to false else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed

{
[ ( ( Stack

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1]

. . .

[0] items

-1 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 )

. . .

70 items

[0]

0 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28)

. . .

28 70 items

[0]

1 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88)

. . .

88 28 70 items

[0]

2 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88) Pop

. . .

88 28 70 items

[0]

1 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95)

. . .

95 28 70 items

[0]

2 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop

. . .

95 28 70 items

[0]

1 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop Pop

. . .

95 28 70 items

[0]

0 top

Stack: Implementation Level


Using an array:
[MAX_ITEMS - 1] Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop Pop Pop

. . .

95 28 70 items

[0]

-1 top

Stack: Implementation Level


Using a linked list:
NULL top

Stack: Implementation Level


Using a linked list:
70 top NULL Push ( 70 )

Stack: Implementation Level


Using a linked list:
28 top 70 NULL Push ( 70 ) Push ( 28 )

Stack: Implementation Level


Using a linked list:
88 top 28 70 NULL Push ( 70 ) Push ( 28 ) Push ( 88 )

Stack: Implementation Level


Using a linked list:
28 top 70 NULL Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop

Stack: Implementation Level


Using a linked list:
95 top 28 70 NULL Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 )

Stack: Implementation Level


Using a linked list:
28 top 70 NULL Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop

Stack: Implementation Level


Using a linked list:
70 top NULL Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop Pop

Stack: Implementation Level


Using a linked list:
NULL top Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop Pop Pop

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.

Queue: Logical Level


Queue Operations: Boolean IsEmpty () Boolean IsFull () void Enqueue (ItemType newitem) void Dequeue (ItemType& newitem)

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

Queue: Application Level


Queues found all over operating system!
I/O buffers Job queues waiting for various resources Spool (print) queue

Queue: Implementation Level


Using an array: Option 1
items [0] front - fixed at [0] (similar to bottom of stack) rear -1 . . . [MAXQUEUE - 1]

Queue: Implementation Level


Using an array: Option 1
items A [0] front - fixed at [0] (similar to bottom of stack) rear 0 Enqueue (A) . . . [MAXQUEUE - 1]

Queue: Implementation Level


Using an array: Option 1
items A [0] front - fixed at [0] (similar to bottom of stack) rear 1 Enqueue (A) Enqueue (B) B . . . [MAXQUEUE - 1]

Queue: Implementation Level


Using an array: Option 1
items A [0] front - fixed at [0] (similar to bottom of stack) rear 2 Enqueue (A) Enqueue (B) Enqueue (C) B C . . . [MAXQUEUE - 1]

Queue: Implementation Level


Using an array: Option 1
items A [0] front - fixed at [0] (similar to bottom of stack) rear 2 Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) B C . . . [MAXQUEUE - 1]

But now front is at position[1] , not [0] Need to shift remaining items down!

Queue: Implementation Level


Using an array: Option 1
items B [0] front - fixed at [0] (similar to bottom of stack) rear 1 Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) C . . . [MAXQUEUE - 1]

After the shifting Is this a very efficient implementation?

Queue: Implementation Level


Using an array: Option 2
items [0] front rear 0 -1 [4] Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 0 0 [4] Enqueue (A) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 0 1 B [4] Enqueue (A) Enqueue (B) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 0 2 B C [4] Enqueue (A) Enqueue (B) Enqueue (C) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 0 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 1 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 2 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items A [0] front rear 2 4 B C D E [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) Hmm . . . Queue now appears full, but there are two unused positions in array! Why not let Queue elements wrap around in array? Note: Let MAXQUEUE = 5 for the example

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items F [0] front rear 2 0 B C D E [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) Enqueue (F) Note: Let MAXQUEUE = 5 for the example Note: to advance the rear indicator : rear = (rear + 1) % MAXQUEUE

Keep track of both front and rear Note: queue is empty when (front 1) == rear

Queue: Implementation Level


Using an array: Option 2
items F [0] front rear 2 1 G C D E [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) Enqueue (F) Enqueue (G) Note: Let MAXQUEUE = 5 for the example Note: to advance the rear indicator : rear = (rear + 1) % MAXQUEUE Now queue REALLY IS full! But look at values of front and rear . . . (front-1) == rear Yikes! This is supposed to mean queue is empty !!!

Keep track of both front and rear Note: queue is empty when (front -1) == rear

Queue: Implementation Level


Using an array: Option 3
items [0] front rear 4 4 [4] front indicates position just before actual front This position must remain unused, effectively reducing size of queue by 1 Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

queue is empty when front == rear


queue is full when (rear + 1) % MAXQUEUE == front (when next Enqueue would put item in unused position.)

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 4 0 [4] Enqueue (A) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 4 1 B [4] Enqueue (A) Enqueue (B) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 4 2 B C [4] Enqueue (A) Enqueue (B) Enqueue (C) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 4 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Note: Let MAXQUEUE = 5 for the example

(Note: queue full)

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 0 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 1 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 2 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Dequeue (ch) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using an array: Option 3
items A [0] front rear 3 3 B C D [4] Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Dequeue (ch) Dequeue (ch) (Note: queue empty) Note: Let MAXQUEUE = 5 for the example

Still keep track of both front and rear

Queue: Implementation Level


Using a linked list:
NULL front NULL rear

Queue: Implementation Level


Using a linked list:
A front NULL

rear

Enqueue (A)

Queue: Implementation Level


Using a linked list:
A front B NULL

rear

Enqueue (A) Enqueue (B)

Queue: Implementation Level


Using a linked list:
A front B C NULL

rear

Enqueue (A) Enqueue (B) Enqueue (C)

Queue: Implementation Level


Using a linked list:
B front C NULL

rear

Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch)

Queue: Implementation Level


Using a linked list:
C front NULL

rear

Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch) Dequeue (ch)

Queue: Implementation Level


Using a linked list:
NULL front NULL rear

Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch) Dequeue (ch) Dequeue (ch)

You might also like