Exercise 15. Stacks and Queues

When working with data structures you’ll oftentimes encounter a structure that is similar to another. A Stack is similar to a SingleLinkedList from Exercise 13, and a Queue is similar to a DoubleLinkedList from Exercise 14. The only difference is a Stack and a Queue restrict the possible operations to simplify how they are used. This helps reduce defects since you can’t accidentally use the Stack like a Queue and cause problems. In a Stack, the nodes are “pushed” onto the “top” and then “popped” from the top as well. With a Queue, the nodes are shifted to the “tail” and then unshifted off the “head” of the structure. Both of these operations are simply simplifications of the SingleLinkedList and DoubleLinkedList ...

Get Learn More Python 3 the Hard Way: The Next Step for New Python Programmers 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.