Vous êtes sur la page 1sur 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 void B ( )
{

this code }

// ------------------------------
outline: void A ( )

B ();

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

int main ( )
{

A ();

return 0;
}
B
Consider the following:
A

main
main begins executing Push (mains ar)
main calls function A Push (As ar) Runtime Stack
function A calls function B Push (Bs ar)
function B returns Use info in Top ar to return control to A
Pop
Use info in Top ar to return control to main
function A returns
Pop
Use info in Top ar to return control to OS
main returns 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
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: Implementation Level
Using an array:
[MAX_ITEMS - 1]

.
.
.

[0] -1

items top
Stack: Implementation Level
Using an array: Push ( 70 )

[MAX_ITEMS - 1]

.
.
.

70 [0] 0

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1]

.
.
.

28
70 [0] 1

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)

.
.
.

88
28
70 [0] 2

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
.
.
.

88
28
70 [0] 1

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
.
.

95
28
70 [0] 2

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
.

95
28
70 [0] 1

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
Pop
.

95
28
70 [0] 0

items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
Pop
.
Pop

95
28
70 [0] -1

items top
Stack: Implementation Level
Using a linked list:

NULL

top
Stack: Implementation Level
Using a linked list: Push ( 70 )

70 NULL

top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )

28 70 NULL

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

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

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

top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
28 70 NULL Pop
Push ( 95 )
Pop
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
70 NULL Pop
Push ( 95 )
Pop
top Pop
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
NULL Pop
Push ( 95 )
Pop
top 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] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)

rear -1
Queue: Implementation Level
Using an array: Option 1

items A . . .

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

Enqueue (A)
rear 0
Queue: Implementation Level
Using an array: Option 1

items A B . . .

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

Enqueue (A)
rear 1 Enqueue (B)
Queue: Implementation Level
Using an array: Option 1

items A B C . . .

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

Enqueue (A)
rear 2 Enqueue (B)
Enqueue (C)
Queue: Implementation Level
Using an array: Option 1

items A B C . . .

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

Enqueue (A)
rear 2 Enqueue (B)
Enqueue (C)
Dequeue(ch)
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 C . . .

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

Enqueue (A)
rear 1 Enqueue (B)
Enqueue (C)
Dequeue(ch)
After the shifting

Is this a very efficient implementation?


Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items

[0] [4]

front 0

rear -1

Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A

[0] [4]

front 0
Enqueue (A)

rear 0

Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B

[0] [4]

front 0
Enqueue (A)
Enqueue (B)
rear 1

Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B C

[0] [4]

front 0
Enqueue (A)
Enqueue (B)
rear 2
Enqueue (C)

Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B C D

[0] [4]

front 0
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B C D

[0] [4]

front 1
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B C D

[0] [4]

front 2
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear
Dequeue (ch)

Note: queue is empty


when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items A B C D E

[0] [4]

front 2 Hmm . . . Queue now


Enqueue (A) appears full, but there
Enqueue (B) are two unused
rear 4
Enqueue (C) positions in array!
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear
Dequeue (ch) Why not let Queue
Enqueue (E) elements wrap
Note: queue is empty around in array?
when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items F B C D E Note: to advance the


rear indicator :

[0] [4] rear = (rear + 1) % MAXQUEUE


front 2
Enqueue (A)
Enqueue (B)
rear 0
Enqueue (C)
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear
Dequeue (ch)
Enqueue (E)
Note: queue is empty Enqueue (F)
when (front 1) == rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 2 for the example

items F G C D E Note: to advance the


rear indicator :

[0] [4] rear = (rear + 1) % MAXQUEUE


front 2
Enqueue (A) Now queue REALLY IS full!
Enqueue (B)
rear 1 But look at values of
Enqueue (C)
Enqueue (D) front and rear . . .
Dequeue (ch)
Keep track of both front and rear
Dequeue (ch) (front-1) == rear
Enqueue (E)
Note: queue is empty Enqueue (F) Yikes! This is supposed to
when (front -1) == rear Enqueue (G) mean queue is empty !!!
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items

[0] [4]

front 4 front indicates position just before actual front

This position must remain unused, effectively


rear 4
reducing size of queue by 1

queue is empty when front == rear


Still keep track of both
front and rear queue is full when (rear + 1) % MAXQUEUE == front
(when next Enqueue would put item in unused position.)
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A

[0] [4]

front 4 Enqueue (A)

rear 0

Still keep track of both


front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B

[0] [4]

front 4 Enqueue (A)


Enqueue (B)
rear 1

Still keep track of both


front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C

[0] [4]

front 4 Enqueue (A)


Enqueue (B)
rear 2 Enqueue (C)

Still keep track of both


front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C D

[0] [4]

front 4 Enqueue (A)


Enqueue (B)
rear 3 Enqueue (C)
Enqueue (D) (Note: queue full)

Still keep track of both


front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C D

[0] [4]

front 0 Enqueue (A)


Enqueue (B)
rear 3 Enqueue (C)
Enqueue (D)
Dequeue (ch)
Still keep track of both
front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C D

[0] [4]

front 1 Enqueue (A)


Enqueue (B)
rear 3 Enqueue (C)
Enqueue (D)
Dequeue (ch)
Still keep track of both Dequeue (ch)
front and rear
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C D

[0] [4]

front 2 Enqueue (A)


Enqueue (B)
rear 3 Enqueue (C)
Enqueue (D)
Dequeue (ch)
Still keep track of both Dequeue (ch)
front and rear Dequeue (ch)
Queue: Implementation Level
Note: Let MAXQUEUE = 5
Using an array: Option 3 for the example

items A B C D

[0] [4]

front 3 Enqueue (A)


Enqueue (B)
rear 3 Enqueue (C)
Enqueue (D)
Dequeue (ch)
Still keep track of both Dequeue (ch)
front and rear Dequeue (ch)
Dequeue (ch) (Note: queue empty)
Queue: Implementation Level
Using a linked list:

NULL

front

NULL

rear
Queue: Implementation Level
Using a linked list:

A NULL

front

Enqueue (A)
rear
Queue: Implementation Level
Using a linked list:

A B NULL

front

rear Enqueue (A)


Enqueue (B)
Queue: Implementation Level
Using a linked list:

A B C NULL

front

rear Enqueue (A)


Enqueue (B)
Enqueue (C)
Queue: Implementation Level
Using a linked list:

B C NULL

front

rear Enqueue (A)


Enqueue (B)
Enqueue (C)
Dequeue (ch)
Queue: Implementation Level
Using a linked list:

C NULL

front

Enqueue (A)
rear Enqueue (B)
Enqueue (C)
Dequeue (ch)
Dequeue (ch)
Queue: Implementation Level
Using a linked list:

NULL

front

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

Vous aimerez peut-être aussi