Chapter 4
Stacks, Queues, and Deques
IN THIS CHAPTER
Adding data to and removing data from the top of a stack
Adding data to one end of a queue and removing it from the other end
Adding or removing data from either end of a deque
Collections and dictionaries are best suited for storing and organizing data, but they aren’t as useful for retrieving data in an orderly fashion. Trying to keep track of data stored by index numbers or by keys can get cumbersome. As a simpler alternative, computer scientists have created three other data structures: stacks, queues, and deques. Unlike collections or dictionaries, these three data structures are designed for storing and removing data in a predictable order.
A list or an array is much simpler to use but much less flexible than a queue or a stack. Unfortunately, stacks, queues, and deques add greater complexity to your program in exchange for their added flexibility.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
If you need to store and remove data that’s often stored and removed within short periods of time, stacks, queues, and deques are better suited than arrays, collections, or dictionaries. ...