Stacks and queues in practice
After this: Use list and deque in Python, ArrayDeque in Java, with their costs in mind.
On this page · 5 sections · 3 checks
You have built a stack and a queue by hand, so you know how they work inside. In real programs you will almost never write these classes yourself, because Python and Java both provide them. Your job is to pick the right one and to keep its costs in mind.
What the languages give you
In Python, a plain list is a good stack, because append and pop both work at the end of the list. For a queue, Python has collections.deque. Its name is said "deck", and it is short for "double-ended queue": you can add and remove quickly at both ends.
In Java, one class does both jobs. ArrayDeque is a stack when you use push and pop, and a queue when you use offer and poll.
from collections import deque
stack = [] # a list is a stack: append and pop work at the endfor v in [1, 2, 3]: stack.append(v)print(stack.pop(), stack)
queue = deque() # a deque is a queue: append at the right, popleft at the leftfor v in [1, 2, 3]: queue.append(v)print(queue.popleft(), queue)3 [1, 2] 1 deque([2, 3])
Deque<Integer> stack = new ArrayDeque<>(); // one class does both jobsfor (int v : new int[] {1, 2, 3}) stack.push(v); // push adds at the headSystem.out.println(stack.pop() + " " + stack); // printed head first: the top is on the left
Deque<Integer> queue = new ArrayDeque<>();for (int v : new int[] {1, 2, 3}) queue.offer(v); // offer adds at the tailSystem.out.println(queue.poll() + " " + queue); // poll takes from the head3 [2, 1] 1 [2, 3]
Each half of the code does the same thing. It adds 1, 2 and 3, takes one value out, then prints that value and what is left. The stack gives back 3, the last in. The queue gives back 1, the first in.
The costs
| Operation | Python | Java (ArrayDeque) | Cost |
|---|---|---|---|
| Push | stack.append(x) on a list | stack.push(x) | amortized O(1) |
| Pop | stack.pop() | stack.pop() | O(1) |
| Peek at the top | stack[-1] | stack.peek() | O(1) |
| Enqueue | queue.append(x) on a deque | queue.offer(x) | O(1) in Python, amortized O(1) in Java |
| Dequeue | queue.popleft() | queue.poll() | O(1) |
| Peek at the front | queue[0] | queue.peek() | O(1) |
| Dequeue from an array | nums.pop(0) on a list | list.remove(0) on an ArrayList | O(n) |
As a reminder, O(1) means the steps stay the same as the data grows. Amortized O(1) means the same thing on average over many operations. O(n) means the work grows with the size of the data. Counting steps has the details.
The last row is the one to remember. It works, and it reads naturally, but it is the slow queue from the queue lesson.
queue = [1, 2, 3]queue.pop(0) # works, but O(n): 2 and 3 both shift leftprint(queue)[2, 3]
List<Integer> queue = new ArrayList<>(List.of(1, 2, 3));queue.remove(0); // works, but O(n): 2 and 3 both shift leftSystem.out.println(queue);[2, 3]
What is behind the names
The rest of this lesson is library detail. You can come back to it later.
Check yourself
Which of these is O(n)?
What do the two print lines show?
from collections import deque
stack = [] # a list is a stack: append and pop work at the endfor v in [1, 2, 3]: stack.append(v)print(stack.pop(), stack)
queue = deque() # a deque is a queue: append at the right, popleft at the leftfor v in [1, 2, 3]: queue.append(v)print(queue.popleft(), queue)Deque<Integer> stack = new ArrayDeque<>(); // one class does both jobsfor (int v : new int[] {1, 2, 3}) stack.push(v); // push adds at the headSystem.out.println(stack.pop() + " " + stack); // printed head first: the top is on the left
Deque<Integer> queue = new ArrayDeque<>();for (int v : new int[] {1, 2, 3}) queue.offer(v); // offer adds at the tailSystem.out.println(queue.poll() + " " + queue); // poll takes from the headIn Java you need a stack of characters for a bracket scan. Which type should you use, and why?
Connect
- Use a Python list as a stack and collections.deque as a queue, and avoid list.pop(0).
- Use ArrayDeque for both jobs in Java, and say why java.util.Stack is left alone.
- Say which stack and queue operations are O(1) and which one is O(n).