Stacks, Queues, & Deques
STACKS
A stack is a crippled list. You may manipulate only the item at the top of the stack. The main operations: you may push
a new item onto the top of the stack; you may pop
the top item off the stack; you may examine the top
item of the stack. A stack can grow arbitrarily large.
1 | public interface Stack { |
In any reasonable implementation, all these methods run in time. A stack is easily implemented as a singly-linked list, using just the front()
, insertFront()
, and removeFront()
methods.
Why talk about Stacks when we already have Lists? Mainly so you can carry on discussions with other computer programmers. If somebody tells you that an algorithm uses a stack, the limitations of a stack give you a hint how the algorithm works.
Sample application: Verifying matched parentheses in a String
like {[(){[]}]()}
. Scan through the String, character by character.
- When you encounter a lefty--’{’, ’[’, or ’(’--push it onto the stack.
- When you encounter a righty, pop its counterpart from atop the stack, and check that they match.
If there’s a mismatch or null returned, or if the stack is not empty when you reach the end of the string, the parentheses are not properly matched.
Stack in Algorithms
Stack: linked-list implementation in Java
1 | public class LinkedStackOfStrings { |
Every operation takes constant time in the worst case.
A stack with N items uses ~ 40 N bytes.
This accounts for the memory for the stack
Stack: array implementation
- Use array
s[]
to store N items on stack. push()
: add new item ats[N]
.pop()
: remove item froms[N-1]
.
1 | public class FixedCapacityStackOfStrings { |
Loitering. Holding a reference to an object when it is no longer needed.
1 | public String pop() { |
1 | public String pop() { |
Stack: resizing-array implementation
1 | public ResizingArrayStackOfStrings() { |
Generic stack: linked-list implementation
1 | public class Stack<Item> { |
1 | public class FixedCapacityStack<Item> { |
QUEUES
A queue is also a crippled list. You may read or remove only the item at the front of the queue, and you may add an item only to the back of the queue. The main operations: you may enqueue
an item at the back of the queue; you may dequeue
the item at the front; you may examine the front
item. Don’t be fooled by the diagram; a queue can grow arbitrarily long.
Sample Application: Printer queues. When you submit a job to be printed at a selected printer, your job goes into a queue. When the printer finishes printing a job, it dequeues the next job and prints it.
1 | public interface Queue { |
In any reasonable implementation, all these methods run in time. A queue is easily implemented as a singly-linked list with a tail pointer.
Queue in Algorithms
1 | public class LinkedQueueOfStrings { |
DEQUES
A deque (pronounced "deck") is a Double-Ended Queue. You can insert and remove items at both ends. You can easily build a fast deque using a doubly-linked list. You just have to add removeFront()
and removeBack()
methods, and deny applications direct access to listnodes. Obviously, deques are less powerful than lists whose listnodes are accessible.
Postscript: A Faster Hash Code (not examinable)
Here’s another hash code for Strings, attributed to one P. J. Weinberger, which has been thoroughly tested and performs well in practice. It is faster than the one above, because it relies on bit operations (which are very fast) rather than the % operator (which is slow by comparison). You will learn about bit operations in CS 61C. Please don’t ask me to explain them to you.
1 | static int hashCode(String key) { |