adt stacks and queues
DESCRIPTION
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 L ast I n F irst O ut ( LIFO ) data structure. Stack: Logical Level. Stack Operations: Boolean IsEmpty () - PowerPoint PPT PresentationTRANSCRIPT
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 codeoutline:
// ------------------------------
void B ( ){
}
// ------------------------------
void A ( )
{
B ();
}
// ------------------------------
int main ( ){
A ();
return 0;}
Consider the following:
main begins executingmain calls function Afunction A calls function Bfunction B returns
function 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
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 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) set balanced to falseelse 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
elseignore symbol
get next symbolif (balanced and stack is empty) well-formedelse ill-formed
( ( { x x x } ) x [ ] x x )
(
(
{
[
Stack
Stack: Implementation Level
Using an array:
7028
.
.
.
items
[0]
[MAX_ITEMS - 1]
1
top
Push ( 70 )Push ( 28)
Stack: Implementation Level
Using an array:
702888
.
.
.
items
[0]
[MAX_ITEMS - 1]
2
top
Push ( 70 )Push ( 28)Push ( 88)
Stack: Implementation Level
Using an array:
702888
.
.
.
items
[0]
[MAX_ITEMS - 1]
1
top
Push ( 70 )Push ( 28)Push ( 88)Pop
Stack: Implementation Level
Using an array:
702895
.
.
.
items
[0]
[MAX_ITEMS - 1]
2
top
Push ( 70 )Push ( 28)Push ( 88)PopPush ( 95)
Stack: Implementation Level
Using an array:
702895
.
.
.
items
[0]
[MAX_ITEMS - 1]
1
top
Push ( 70 )Push ( 28)Push ( 88)PopPush ( 95)Pop
Stack: Implementation Level
Using an array:
702895
.
.
.
items
[0]
[MAX_ITEMS - 1]
0
top
Push ( 70 )Push ( 28)Push ( 88)PopPush ( 95)PopPop
Stack: Implementation Level
Using an array:
702895
.
.
.
items
[0]
[MAX_ITEMS - 1]
-1
top
Push ( 70 )Push ( 28)Push ( 88)PopPush ( 95)PopPopPop
Stack: Implementation Level
Using a linked list:
top
NULL
Push ( 70 )Push ( 28 )Push ( 88 )
88 28 70
Stack: Implementation Level
Using a linked list:
top
NULL
Push ( 70 )Push ( 28 )Push ( 88 )PopPush ( 95 )
95 28 70
Stack: Implementation Level
Using a linked list:
top
NULL28 70
Push ( 70 )Push ( 28 )Push ( 88 )PopPush ( 95 )Pop
Stack: Implementation Level
Using a linked list:
top
NULL70
Push ( 70 )Push ( 28 )Push ( 88 )PopPush ( 95 )PopPop
Stack: Implementation Level
Using a linked list:
top
NULL
Push ( 70 )Push ( 28 )Push ( 88 )PopPush ( 95 )PopPopPop
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]
-1
. . .
rear
front - fixed at [0] (similar to bottom of stack)
Queue: Implementation Level
Using an array: Option 1
items
[0] [MAXQUEUE - 1]
0
A . . .
rear
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
Queue: Implementation Level
Using an array: Option 1
items
[0] [MAXQUEUE - 1]
1
A B . . .
rear
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)Enqueue (B)
Queue: Implementation Level
Using an array: Option 1
items
[0] [MAXQUEUE - 1]
2
A B C . . .
rear
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)Enqueue (B)Enqueue (C)
Queue: Implementation Level
Using an array: Option 1
items
[0] [MAXQUEUE - 1]
2
A B C . . .
rear
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)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
[0] [MAXQUEUE - 1]
1
B C . . .
rear
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)Enqueue (B)Enqueue (C)Dequeue(ch)
After the shifting
Is this a very efficient implementation?
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
-1rear
front 0
Note: Let MAXQUEUE = 5for 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
[0] [4]
0
A
rear
front 0
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
1
A B
rear
front 0
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
2
A B C
rear
front 0
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
3
A B C D
rear
front 0
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
3
A B C D
rear
front 1
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
3
A B C D
rear
front 2
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
4
A B C D E
rear
front 2
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
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?
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
0
F B C D E
rear
front 2
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front – 1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)Enqueue (E)Enqueue (F)
Note: to advance the rear indicator :
rear = (rear + 1) % MAXQUEUE
Queue: Implementation Level
Using an array: Option 2
items
[0] [4]
1
F G C D E
rear
front 2
Note: Let MAXQUEUE = 5for the example
Keep track of both front and rear
Note: queue is empty when (front -1) == rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)Enqueue (E)Enqueue (F)Enqueue (G)
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 !!!
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
4rear
front 4
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
queue is empty when front == rear
front indicates position just before actual front
queue is full when (rear + 1) % MAXQUEUE == front(when next Enqueue would put item in unused position.)
This position must remain unused, effectively reducing size of queue by 1
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
0
A
rear
front 4
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
1
A B
rear
front 4
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
2
A B C
rear
front 4
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
3
A B C D
rear
front 4
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D) (Note: queue full)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
3
A B C D
rear
front 0
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
3
A B C D
rear
front 1
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
3
A B C D
rear
front 2
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)Dequeue (ch)
Queue: Implementation Level
Using an array: Option 3
items
[0] [4]
3
A B C D
rear
front 3
Note: Let MAXQUEUE = 5for the example
Still keep track of both front and rear
Enqueue (A)Enqueue (B)Enqueue (C)Enqueue (D)Dequeue (ch)Dequeue (ch)Dequeue (ch)Dequeue (ch) (Note: queue empty)
Queue: Implementation LevelUsing a linked list:
front
NULL
rear
A
Enqueue (A)Enqueue (B)Enqueue (C)
B C
Queue: Implementation LevelUsing a linked list:
front
NULL
rear
B
Enqueue (A)Enqueue (B)Enqueue (C)Dequeue (ch)
C
Queue: Implementation LevelUsing a linked list:
front
NULL
rear
C
Enqueue (A)Enqueue (B)Enqueue (C)Dequeue (ch)Dequeue (ch)