1 / 28

Stacks and Queues

Stacks and Queues. Stacks and Queues. Not really “data structures” More of an enforcement of policy Can be implemented using an array or linked list Can store just about any kind of data Queues First In, First Out (FIFO) Like waiting in line Stacks First In, Last Out (FILO)

elom
Download Presentation

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. Stacks and Queues

  2. Stacks and Queues • Not really “data structures” • More of an enforcement of policy • Can be implemented using an array or linked list • Can store just about any kind of data • Queues • First In, First Out (FIFO) • Like waiting in line • Stacks • First In, Last Out (FILO) • Like a stack of trays

  3. Stacks • Three primary operations • Push() – put new data on the top of the stack • Pop() – remove data from the top of the stack • Peek() – get a copy of the data on the top of the stack • Useful • our function stack! • stack of cards, tiles, loot, etc…

  4. Example(pushing) 5 Note: could use a linked list also

  5. Example(pushing) 11 5

  6. Example(pushing) -6 11 5

  7. Example(pushing) 45 -6 11 5

  8. Example(current stack) 45 -6 11 5

  9. Example(peeking) 45 45 -6 11 5

  10. Example(popping) 45 -6 11 5

  11. Example(pushing) 31 -6 11 5

  12. Example(popping) 31 -6 11 5

  13. Example(popping) -6 11 5

  14. Example(popping) 11 5

  15. Example(popping) FILO 5

  16. Queues • Two primary operations • Enqueue() – put new data at the end of the queue • Dequeue() – remove data from the beginning of the queue • Peek() – yes, it’s still there… • Useful • Enforcing fairness (waitlist at SPSU) • Player turns during a round

  17. Example

  18. Example(enqueue a 5) 5

  19. Example 5

  20. Example(enqueue an 11) 11 5

  21. Example 5 11

  22. Example(enqueue a -6) -6 5 11

  23. Example 5 11 -6

  24. Example(dequeue) 5 11 -6

  25. Example(dequeue) 5 11 -6

  26. Example(dequeue) 11 5 -6 FIFO

  27. Example(dequeue) 11 -6

  28. Summary • Not really “data structures” • More of an enforcement of policy • Can be implemented using an array or linked list • Queues are FIFO • Stacks are FILO • Which data structure is LILO? • Which one is LIFO?

More Related