Stacks and Queues
Stacks and queues: the two data structures defined by the order items leave in, last in first out or first in first out, and how to build each cheaply.
A stack hands back the newest item it holds and a queue hands back the oldest, and that order is all that defines either one. It matters because many problems ask for one order or the other, from undo and bracket checking to printers and servers, and because a queue built the obvious way on an array quietly turns O(n) work into O(n²). The topic starts with the two orders and their operations, and how to trace a sequence of them to the state it leaves. It then shows the cue for each in an example, brackets for a stack and print jobs for a queue, and the built-ins to use in Python, Java and TypeScript. It ends with the cost of dequeuing from the front of an array, and the ring buffer, which makes a queue of fixed size from an array without shifting anything.
Contents
9 cardsnot started