adt stacks and queues. stack: logical level “an ordered group of homogeneous items or elements in...
TRANSCRIPT
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:void clear()void push (E it)E pop ()E topValue ()int length();
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:
Public class SomeMethods (){
// ------------------------------public static void B ( ){
}
// ------------------------------public static void A ( ){
B ();
}
// ------------------------------public static void main (String[] args ){
A ();
}}
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
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) // 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
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
Stack: Implementation Level
Using an array:
70
.
.
.
listArray
[0]
[MAX_ITEMS - 1]
1
top
push ( 70 )
10
maxSize
Stack: Implementation Level
Using an array:
7028
.
.
.
listArray
[0]
[MAX_ITEMS - 1]
2
top
push ( 70 )push ( 28)
10
maxSize
Stack: Implementation Level
Using an array:
702888
.
.
.
listArray
[0]
[MAX_ITEMS - 1]
3
top
push ( 70 )push ( 28)push ( 88)
10
maxSize
Stack: Implementation Level
Using an array:
702888
.
.
.
listArray
[0]
[MAX_ITEMS - 1]
2
top
push ( 70 )push ( 28)push ( 88)pop ()
10
maxSize
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
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
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
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
Stack: Implementation Level
Using a linked list:
top
NULL
0
size
Stack: Implementation Level
Using a linked list:
top
NULL
push ( 70 )
70
1
size
Stack: Implementation Level
Using a linked list:
top
NULL
push ( 70 )push ( 28 )
28 70
2
size
Stack: Implementation Level
Using a linked list:
top
NULL
push ( 70 )push ( 28 )push ( 88 )
88 28 70
3
size
Stack: Implementation Level
Using a linked list:
top
NULL28 70
push ( 70 )push ( 28 )push ( 88 )pop ()
2
size
Stack: Implementation Level
Using a linked list:
top
NULL
push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )
95 28 70
3
Stack: Implementation Level
Using a linked list:
top
NULL28 70
push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()
2
size
Stack: Implementation Level
Using a linked list:
top
NULL70
push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()pop ()
1
size
Stack: Implementation Level
Using a linked list:
top
NULL
push ( 70 )push ( 28 )push ( 88 )pop ()push ( 95 )pop ()pop ()pop ()
0
size
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:void clear ()void enqueue (E it)E dequeue ()E frontValue ()int length()
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] [maxSize - 1]
0
. . .
rear
front - fixed at [0] (similar to bottom of stack)
*Note that rear indicates position where next enqueued item will go
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)
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)
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)
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!
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)
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)
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
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
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
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
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
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?
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
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
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
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!
Queue: Implementation LevelUsing a linked list:
front
NULL
rear
0size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear enqueue (A)
A
1size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear enqueue (A)enqueue (B)
A B
2size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear enqueue (A)enqueue (B)enqueue (C)
A B C
3size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear enqueue (A)enqueue (B)enqueue (C)dequeue ()
B C
2size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear enqueue (A)enqueue (B)enqueue (C)dequeue ()dequeue ()
C
0size
Queue: Implementation LevelUsing a linked list:
front
NULL
rear
enqueue (A)enqueue (B)enqueue (C)dequeue ()dequeue ()dequeue ()0size