550 likes | 637 Views
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: void clear()
E N D
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: B A Push (main’s ar) Push (A’s ar) Push (B’s 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 main main begins executing main calls method A method A calls method B method B returns method A returns main returns 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 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) // check that length is 0 set balanced to false else pop the stack to get top opening symbol 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 ( [ ( { Popped { ( [ ( Stack
Stack: Implementation Level Using an array: [maxSize - 1] . . . *Note that top indicates position where next pushed item will go [0] 0 10 listArray top* maxSize
Stack: Implementation Level Using an array: push ( 70 ) [MAX_ITEMS - 1] . . . 70 [0] 1 10 top maxSize listArray
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) [MAX_ITEMS - 1] . . . 28 70 [0] 2 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) [MAX_ITEMS - 1] . . . 88 28 70 [0] 3 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () [MAX_ITEMS - 1] . . . 88 28 70 [0] 2 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) [MAX_ITEMS - 1] . . . 95 28 70 [0] 3 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 2 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 1 10 listArray top maxSize
Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () pop () pop () [MAX_ITEMS - 1] . . . 95 28 70 [0] 0 10 listArray top maxSize
Stack: Implementation Level Using a linked list: NULL top 0 size
Stack: Implementation Level Using a linked list: push ( 70 ) 70 NULL top 1 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) 88 28 70 NULL top 3 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 95 28 70 NULL top 3
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 28 70 NULL top 2 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () 70 NULL top 1 size
Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () pop () NULL top 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] front - fixed at [0] (similar to bottom of stack) rear 0 *Note that rear indicates position where next enqueued item will go
Queue: Implementation Level Using an array: Option 1 A . . . items [0] [maxSize- 1] front - fixed at [0] (similar to bottom of stack) enqueue(A) rear 1
Queue: Implementation Level Using an array: Option 1 A B . . . items [0] [maxSize- 1] front - fixed at [0] (similar to bottom of stack) enqueue(A) enqueue(B) rear 2
Queue: Implementation Level Using an array: Option 1 A B C . . . items [0] [maxSize- 1] front - fixed at [0] (similar to bottom of stack) enqueue(A) enqueue(B) enqueue(C) rear 3
Queue: Implementation Level Using an array: Option 1 A B C . . . items [0] [maxSize- 1] front - fixed at [0] (similar to bottom of stack) enqueue(A) enqueue(B) enqueue(C) dequeue() rear 3 But now front is at position[1] , not [0] Need to shift remaining items down!
Queue: Implementation Level Using an array: Option 1 B C . . . items [0] [maxSize- 1] front - fixed at [0] (similar to bottom of stack) enqueue(A) enqueue(B) enqueue(C) dequeue() rear 2 After the shifting Is this a very efficient implementation? Θ(n)
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 items [0] [4] front front is actual front (except when empty) 1 rear is actual rear (except when empty) rear 0 Keep track of both front and rear Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A items [0] [4] front 1 enqueue(A) rear 1 Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A B items [0] [4] front 1 enqueue(A) enqueue(B) rear 2 Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A B C items [0] [4] front 1 enqueue(A) enqueue(B) enqueue(C) rear 3 Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A B C D items [0] [4] front Hmm . . . Queue now appears full . . . but 1 enqueue(A) enqueue(B) enqueue(C) enqueue(D) rear 4 Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A B C D items [0] [4] front 2 enqueue(A) enqueue(B) enqueue(C) enqueue(D) dequeue () rear 4 Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 A C D B items What if we want to Enqueue a couple of more items? [0] [4] front 3 enqueue(A) enqueue(B) enqueue(C) enqueue(D) dequeue () dequeue () rear 4 Why not let Queue elements “wrap around” in array? Note that: length = rear – front + 1
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 E A B C D Note: to advance the rear indicator : items rear = (rear + 1) % maxSize [0] [4] front 3 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue() Dequeue() Enqueue (E) rear 0 Note that: length = rear – front + 1 ??? correction: length = ((rear+maxSize) – front + 1) % maxSize
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 E F B C D items [0] [4] front 3 enqueue(A) enqueue(B) enqueue(C) enqueue(D) dequeue () dequeue () enqueue(E) enqueue(F) rear 1 remember: length = ((rear+maxSize) – front + 1) % maxSize
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 E F G C D items [0] [4] front 3 enqueue(A) enqueue(B) enqueue(C) enqueue(D) dequeue () dequeue () enqueue(E) enqueue(F) enqueue(G) rear 2 remember: length = ((rear+maxSize) – front + 1) % maxSize
Queue: Implementation Level Note: Let maxSize= 5 for the example Using an array: Option 2 E F G C D Also Note: to advance the rear or front indicators: items rear = (rear + 1) % maxSize front = (front+1) % maxSize [0] [4] front 3 Now queue REALLY IS full! rear 2 But look at values of front and rear and . . . remember: length = ((rear+maxSize) – front + 1) % maxSize length = ((2 + 5) – 3 + 1) % 5 = 0 Oops! This is supposed to mean queue is empty !!! ??? Solution: Don’t let this happen; “waste” an array position!
Queue: Implementation Level Using a linked list: NULL front rear size 0
Queue: Implementation Level Using a linked list: A NULL front enqueue(A) rear size 1
Queue: Implementation Level Using a linked list: A B NULL front enqueue(A) enqueue(B) rear size 2