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 code outline: Public class SomeMethods () { // public static void B ( ) { } // public static void A ( ) { B (); } // public static void main (String[] args ) { A (); }
Consider the following: main begins executing main calls method A method A calls method B method 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 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 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 Algorithm 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 ( ( { 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: listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) 10 maxSize
Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) 10 maxSize
Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) 10 maxSize
Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 2 top push ( 70 ) push ( 28) push ( 88) pop () 10 maxSize
Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 3 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) 10 maxSize
Stack: Implementation Level Using an array: 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: listArray [0] [MAX_ITEMS - 1] 1 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) pop () 10 maxSize
Stack: Implementation Level Using an array: listArray [0] [MAX_ITEMS - 1] 0 top push ( 70 ) push ( 28) push ( 88) pop () push ( 95) 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 ) size
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) size
Stack: Implementation Level Using a linked list: top NULL 2870 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 )
Stack: Implementation Level Using a linked list: top NULL 2870 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 2 size
Stack: Implementation Level Using a linked list: top NULL 70 push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 1 size
Stack: Implementation Level Using a linked list: top NULL push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 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 AB... 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 ABC... 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 ABC... 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 BC... 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] 0 rear front 1 Note: Let maxSize = 5 for 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 = 5 for the example enqueue (A) Note that: length = rear – front + 1
Queue: Implementation Level Using an array: Option 2 items [0][4] 2 AB rear front 1 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) Note that: length = rear – front + 1
Queue: Implementation Level Using an array: Option 2 items [0][4] 3 ABC rear front 1 Note: Let maxSize = 5 for 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 ABCD rear front 1 Note: Let maxSize = 5 for 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 ABCD rear front 2 Note: Let maxSize = 5 for 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 CD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) 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 EABCD rear front 3 Note: Let maxSize = 5 for the example Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) 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 EFBCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) dequeue () enqueue (E) enqueue (F) remember: length = ((rear+maxSize) – front + 1) % maxSize
Queue: Implementation Level Using an array: Option 2 items [0][4] 2 EFGCD rear front 3 Note: Let maxSize = 5 for the example enqueue (A) enqueue (B) enqueue (C) enqueue (D) 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 EFGCD rear front 3 Note: Let maxSize = 5 for the example Also Note: to advance the rear or front indicators: rear = (rear + 1) % maxSize front = (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 Level Using a linked list: front NULL rear 0 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) A 1 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) AB 2 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) ABC 3 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () BC 2 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () C 0 size
Queue: Implementation Level Using a linked list: front NULL rear enqueue (A) enqueue (B) enqueue (C) dequeue () 0 size