Sedgewick & Wayne · §4.3 Stacks and Queues · pp. 571-596
Linked Lists, Resizing Arrays and Stack Clients
The figures of §4.3, one step at a time. Build a linked list and insert and remove at its beginning, run LinkedStackOfStrings and the resizing array on tobe.txt, evaluate an expression with two stacks (or with one, in postfix), and trace a FIFO queue built from a linked list.
1 · A linked list, one assignment at a time
A Node holds an item and a reference next to the next node; the last node's next is null. Build the list to be or, then insert a node at the beginning, remove the first node, or walk through the list. Every Step runs one line. Book pp. 571-574.
Operation
Item to insert
Objects in memory
Code
s
2 · LinkedStackOfStrings on tobe.txt
push() inserts a node at the beginning of the list, pop() removes the first node: the top of the stack is always first. There is no capacity: every push creates a node. A popped node has no reference to it any more (an orphan, dashed), and Java's garbage collector reclaims its memory. Change the words to try your own. Program 4.3.2, trace p. 576.
Input
The linked list
StdOut
Program 4.3.2
items on stack0
nodes created0
memory of the nodes0 bytes
3 · ResizingArrayStackOfStrings: double when full, halve at one-quarter
The array starts with length 1. push() doubles the length when the array is full, pop() halves it when the stack is down to one-quarter of the length. Each resize copies all n items into a new array, so one push can be expensive - but on average every operation costs a constant number of copies. Switch to halving at one-half and push and pop at the boundary to see why the book waits for one-quarter. Program 4.3.3, trace p. 580.
Halve the array when n is
Word
What happened
Program 4.3.3
n0
items.length1
operations0
items copied (resizes)0
copies per operation0
4 · Evaluating an expression with stacks
Dijkstra's two-stack algorithm (book) reads a fully parenthesized expression: it ignores (, pushes operators on one stack and numbers on the other, and at every ) pops an operator and its operands and pushes the result. Postfix (reverse Polish notation, from the CS.12 lecture) needs no parentheses and only one stack: a number is pushed, an operator pops its operands and pushes the result. Separate the tokens with spaces. Program 4.3.5, trace p. 589.
Notation
Expression
ops
values
Program 4.3.5
5 · A FIFO queue as a linked list
The queue keeps two references: first (the oldest item, where dequeue() removes) and last (the newest, where enqueue() links a new node). dequeue() works like the stack's pop(); enqueue() inserts at the end. Watch the two special cases: an enqueue into an empty queue sets first too, and the dequeue of the last item sets last to null. Step runs one line, Next operation a whole enqueue or dequeue. Program 4.3.6, pp. 592-595.
Input
The linked list
StdOut
Program 4.3.6
Built to accompany COSC 2230 (Computer Science III), Sedgewick & Wayne, Introduction to Programming in Java, 2nd ed., §4.3. The code panels follow Programs 4.3.2, 4.3.3, 4.3.5 and 4.3.6 and the postfix evaluator of the CS.12 lecture; everything is computed live in the browser.