1 / 54

ADT Stacks and Queues

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()

Télécharger la présentation

ADT Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ADT Stacks and Queues

  2. 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.

  3. Stack: Logical Level Stack Operations: void clear() void push (E it) E pop () E topValue () int length();

  4. 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

  5. Consider this codeoutline: Public class SomeMethods () { // ------------------------------ public static void B ( ) { } // ------------------------------ public static void A ( ) { B (); } // ------------------------------ public static void main (String[] args ) { A (); } }

  6. 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

  7. 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

  8. 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

  9. 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

  10. Stack: Implementation Level Using an array: push ( 70 ) [MAX_ITEMS - 1] . . . 70 [0] 1 10 top maxSize listArray

  11. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) [MAX_ITEMS - 1] . . . 28 70 [0] 2 10 listArray top maxSize

  12. Stack: Implementation Level Using an array: push ( 70 ) push ( 28) push ( 88) [MAX_ITEMS - 1] . . . 88 28 70 [0] 3 10 listArray top maxSize

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Stack: Implementation Level Using a linked list: NULL top 0 size

  19. Stack: Implementation Level Using a linked list: push ( 70 ) 70 NULL top 1 size

  20. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) 28 70 NULL top 2 size

  21. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) 88 28 70 NULL top 3 size

  22. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () 28 70 NULL top 2 size

  23. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) 95 28 70 NULL top 3

  24. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () 28 70 NULL top 2 size

  25. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () 70 NULL top 1 size

  26. Stack: Implementation Level Using a linked list: push ( 70 ) push ( 28 ) push ( 88 ) pop () push ( 95 ) pop () pop () pop () NULL top 0 size

  27. 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.

  28. Queue: Logical Level Queue Operations: void clear () void enqueue (E it) E dequeue () E frontValue () int length()

  29. 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

  30. Queue: Application Level • Queues found all over operating system! • I/O buffers • Job queues waiting for various resources • Spool (print) queue

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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!

  36. 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)

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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!

  48. Queue: Implementation Level Using a linked list: NULL front rear size 0

  49. Queue: Implementation Level Using a linked list: A NULL front enqueue(A) rear size 1

  50. Queue: Implementation Level Using a linked list: A B NULL front enqueue(A) enqueue(B) rear size 2

More Related