Course 3.1Lesson 1 of 5
A stack is a pile where the last item put on is the first taken off, and a Python list does the job with append to push and pop to take the top, and an empty stack is a list that is False. Stacks fit jobs that need to go back, such as undo, reversing, and checking that brackets are balanced. A queue is a waiting line where the first to arrive is served first, and collections.deque makes one that is fast at both ends, with append to join and popleft to serve. A list is slow as a queue, because pop of 0 moves every other item. Choose the structure by how items come out: last in first out, or first in first out.
Try the code and the simulations beside the lesson.
After this lesson you can
collections.deque as a queue, andYou know lists, dictionaries, tuples and sets. They are general. But for some jobs, a simpler shape is better, because its rules keep you from doing the wrong thing, and say clearly what the program does. A data structure is a way of organising data together with the actions that are allowed on it. In this chapter you meet five. The first two are the simplest: the stack and the queue.
Think of a pile of plates. You put a plate on top, and you take a plate from the top. The last plate that you put on is the first that comes off. This is LIFO: last in, first out.
A Python list is a good stack. append pushes onto the top, and pop() takes the top off.
Predict first. What do the two pop() calls show, and what does the empty stack do? Then run it.
stack.append(x). pop is stack.pop(), which removes the last item and gives it back. peek is stack[-1]: look at the top without removing.while stack: is a short way to say "while the stack is not empty", because an empty list is False.stack.pop() on an empty stack raises an IndexError. Always check, or catch it.stack becomes kcats.Hello there! and then undoes twice, getting back Hello there and then Hello. This is how the Undo button in every editor works.How does a computer check that (a + b) * [c - d] has its brackets in order, and that {[(])} does not? A stack does it.
Test: ((a) leaves one ( open: False. a)( has a closing bracket with nothing to close: False. {[(])} has ] where ) is expected: False. An empty text, and a text with no brackets, are balanced: True. A compiler uses this idea to check every program that you write.
A waiting line at a bus stop is a queue. The person who came first is served first. This is FIFO: first in, first out. New arrivals join at the back, and the person at the front leaves.
A list is a poor queue, because list.pop(0) takes the first item, and then every other item moves one place forward. For a long list, that is slow. Python has a better tool: collections.deque (say "deck": a double-ended queue). It is fast at both ends.
A readout of 0 means the structure is empty: the next pop() or popleft() would fail.
| Stack | Queue | |
|---|---|---|
| Order | last in, first out | first in, first out |
| Add at | the top | the back |
| Remove from | the top | the front |
| Python tool | list (append, pop) | deque (append, popleft) |
| Typical jobs | undo, going back, brackets, depth-first search, a call stack | waiting lines, printing, messages, breadth-first search |
Ask: "which item should come out next?" The newest: a stack. The oldest: a queue.
IndexError. Test first.list.pop(0) for a big queue. It is slow. Use deque.pop() also removes. Use stack[-1] to only look.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
balanced, and the checker tries good and bad texts.deque does it, with append and popleft.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
pop() give?deque better than a list for a queue?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.