1 / 62

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: Boolean IsEmpty ()

Download Presentation

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: Boolean IsEmpty () Boolean IsFull () Push (ItemType newitem) void Pop () ItemType Top ()

  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: // ------------------------------ void B ( ) { } // ------------------------------ void A ( ) { B (); } // ------------------------------ int main ( ) { A (); return 0; }

  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 function A function A calls function B function B returns function 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) set balanced to false else 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 else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed { ( [ ( Stack

  9. Stack: Implementation Level Using an array: [MAX_ITEMS - 1] . . . [0] -1 items top

  10. Stack: Implementation Level Using an array: Push ( 70 ) [MAX_ITEMS - 1] . . . 70 [0] 0 items top

  11. Stack: Implementation Level Using an array: Push ( 70 ) Push ( 28) [MAX_ITEMS - 1] . . . 28 70 [0] 1 items top

  12. Stack: Implementation Level Using an array: Push ( 70 ) Push ( 28) Push ( 88) [MAX_ITEMS - 1] . . . 88 28 70 [0] 2 items top

  13. Stack: Implementation Level Using an array: Push ( 70 ) Push ( 28) Push ( 88) Pop [MAX_ITEMS - 1] . . . 88 28 70 [0] 1 items top

  14. Stack: Implementation Level Using an array: Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) [MAX_ITEMS - 1] . . . 95 28 70 [0] 2 items top

  15. Stack: Implementation Level Using an array: Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95) Pop [MAX_ITEMS - 1] . . . 95 28 70 [0] 1 items top

  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] 0 items top

  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] -1 items top

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

  19. Stack: Implementation Level Using a linked list: Push ( 70 ) 70 NULL top

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

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

  22. Stack: Implementation Level Using a linked list: Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop 28 70 NULL top

  23. Stack: Implementation Level Using a linked list: Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) 95 28 70 NULL top

  24. Stack: Implementation Level Using a linked list: Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop 28 70 NULL top

  25. Stack: Implementation Level Using a linked list: Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop Pop 70 NULL top

  26. Stack: Implementation Level Using a linked list: Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95 ) Pop Pop Pop NULL top

  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: Boolean IsEmpty () Boolean IsFull () void Enqueue (ItemType newitem) void Dequeue (ItemType& newitem)

  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] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) rear -1

  32. Queue: Implementation Level Using an array: Option 1 A . . . items [0] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) Enqueue (A) rear 0

  33. Queue: Implementation Level Using an array: Option 1 A B . . . items [0] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) rear 1

  34. Queue: Implementation Level Using an array: Option 1 A B C . . . items [0] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) Enqueue (C) rear 2

  35. Queue: Implementation Level Using an array: Option 1 A B C . . . items [0] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) rear 2 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] [MAXQUEUE - 1] front - fixed at [0] (similar to bottom of stack) Enqueue (A) Enqueue (B) Enqueue (C) Dequeue(ch) rear 1 After the shifting Is this a very efficient implementation?

  37. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 items [0] [4] front 0 rear -1 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  38. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A items [0] [4] front 0 Enqueue (A) rear 0 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  39. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A B items [0] [4] front 0 Enqueue (A) Enqueue (B) rear 1 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  40. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A B C items [0] [4] front 0 Enqueue (A) Enqueue (B) Enqueue (C) rear 2 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  41. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A B C D items [0] [4] front 0 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) rear 3 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  42. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A B C D items [0] [4] front 1 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) rear 3 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  43. Queue: Implementation Level Note: Let MAXQUEUE = 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 (ch) Dequeue (ch) rear 3 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  44. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 A B C D E items [0] [4] front Hmm . . . Queue now appears full, but there are two unused positions in array! 2 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) rear 4 Keep track of both front and rear Why not let Queue elements “wrap around” in array? Note: queue is empty when (front – 1) == rear

  45. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 F B C D E Note: to advance the rear indicator : items [0] [4] rear = (rear + 1) % MAXQUEUE front 2 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) Enqueue (F) rear 0 Keep track of both front and rear Note: queue is empty when (front – 1) == rear

  46. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 2 F G C D E Note: to advance the rear indicator : items [0] [4] rear = (rear + 1) % MAXQUEUE front 2 Enqueue (A) Enqueue (B) Enqueue (C) Enqueue (D) Dequeue (ch) Dequeue (ch) Enqueue (E) Enqueue (F) Enqueue (G) Now queue REALLY IS full! rear 1 But look at values of front and rear . . . Keep track of both front and rear (front-1) == rear Yikes! This is supposed to mean queue is empty !!! Note: queue is empty when (front -1) == rear

  47. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 3 items [0] [4] front front indicates position just beforeactual front 4 This position must remain unused, effectively reducing size of queue by 1 rear 4 queue is empty when front == rear Still keep track of both front and rear queue is full when (rear + 1) % MAXQUEUE == front (when next Enqueue would put item in unused position.)

  48. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 3 A items [0] [4] front 4 Enqueue (A) rear 0 Still keep track of both front and rear

  49. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 3 A B items [0] [4] front 4 Enqueue (A) Enqueue (B) rear 1 Still keep track of both front and rear

  50. Queue: Implementation Level Note: Let MAXQUEUE = 5 for the example Using an array: Option 3 A B C items [0] [4] front 4 Enqueue (A) Enqueue (B) Enqueue (C) rear 2 Still keep track of both front and rear

More Related