Python Programming · High School (11–12) · 1 course, 11 worked problems, 20 practice questions
A data structure is a way of organising data so that a program can use it well. Python's lists and dictionaries are data structures, and others can be built from them. A stack works last in, first out, like a pile of plates: you push onto the top and pop from the top. A queue works first in, first out, like a line at a counter. A linked list joins items by references, and a tree joins them in branches, as in a folder system. A graph joins items in a network, such as friends who know each other.
Choose an answer, then check it. Each one shows a full explanation.
Push 1, 2 and 3 on a stack, pop once, then push 4. What does the next pop() give?
A queue gets A, B and C in that order. popleft() runs twice. Who is at the front now?
5, 2 and 8 are inserted into an empty binary search tree. Where is 8?
A linked list has 6 nodes. How many next links does a walk from the head follow to reach node 6?
s = [] and s.append(1), s.append(2), s.append(3), then s.pop(). What is s?
from collections import deque, q = deque([1, 2, 3]), then q.popleft(). What is list(q)?
A stack holds [1, 2, 3] with 3 on top. What does the loop while s: print(s.pop()) show?
A queue of A, B, C: A is served, then D joins. Who is served next?
A linked list has the nodes 1, 2, 3. A node X is pushed at the front. What is the list in order?
Values 4, 2, 6 are inserted in that order into an empty binary search tree. What does the in-order walk give?
Each one has a hint, the key idea and a full worked solution.