Chapter 4

Stacks, Queues, and Deques

IN THIS CHAPTER

Bullet Adding data to and removing data from the top of a stack

Bullet Adding data to one end of a queue and removing it from the other end

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

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

Get Beginning Programming All-in-One For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.