MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 9 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python11 min · Section 9 of 12

10.9Arrays, Linked Lists, Stacks and Queues

The programme guide lists arrays, linked lists, stacks and queues for this unit. You will study them in depth in MCS-208 (Data Structures and Algorithms); here you see how each is built in Python using the tools of this unit. The printed unit does not include this material, so this section has no study-material section numbers.

Arrays

An array is a sequence of items of the same type stored next to each other in memory, accessed by index in constant time. In Python:

OptionWhat it isUse when
listA dynamic array of references to objects of any typeAlmost always
array.array (standard library)A compact array of one numeric type, like a C array, e.g. array('i', [1, 2, 3])Many numbers, memory matters
NumPy ndarrayFast multi-dimensional numeric arrayNumerical computing

Linked lists

A linked list is a chain of nodes, where each node stores a value and a reference to the next node. Inserting at the front takes constant time (no shifting), but reaching item i means walking from the head. Python has no built-in linked list, so we build nodes with a small class. (Classes are covered fully in Unit 13; here a class is just a template for an object with two fields.)

A linked list holding 30, 20, 10
head
 │
 ▼
[30 | ●]──►[20 | ●]──►[10 | None]

Stacks

A stack supports push (add to the top) and pop (remove from the top). A Python list is a ready-made stack: append() pushes and pop() pops, both in constant time at the end of the list. Stacks are used for undo features, browser back buttons, expression evaluation and function calls.

Queues

A queue is First In, First Out (FIFO), like a ticket counter line: enqueue at the rear, dequeue from the front. Use collections.deque (a double-ended queue): append() adds at the rear and popleft() removes from the front, both in constant time. A list’s pop(0) also works but is slow for long lists, because every remaining item shifts one place left.

StructureOrderPython toolAddRemove
StackLIFOlistappend(x)pop()
QueueFIFOcollections.dequeappend(x)popleft()
Linked listLink orderYour own Node classLink a new nodeRelink around a node
ArrayBy indexlist, array.arrayappend, insertpop, del

Key takeaways

  • A list is a dynamic array; array.array stores one numeric type compactly.
  • A linked list is nodes joined by references; insertion at the front is constant time.
  • Stack = LIFO with append/pop; queue = FIFO with deque.append/popleft.
  • These structures are studied in depth in MCS-208.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which principle does a stack follow?

Show answer

Answer: B. Last In, First Out

The most recently pushed item is the first popped, like a pile of plates. FIFO describes a queue.

Question 2 · multiple choice

Which Python operations implement push and pop on a list used as a stack?

Show answer

Answer: A. append(x) and pop()

Both work at the end of the list in constant time. Lists have no push or add method.

Question 3 · multiple choice

What does this program print?

Python · quiz_stack.py
s = []
for x in [1, 2, 3]:
    s.append(x)
s.pop()
s.append(4)
print(s.pop(), s)
Show answer

Answer: C. 4 [1, 2]

After pushing 1, 2, 3 and popping 3, the stack is [1, 2]; pushing 4 and popping returns 4, leaving [1, 2].

Question 4 · multiple choice

What does this program print?

Python · quiz_queue.py
from collections import deque
q = deque([5, 6])
q.append(7)
q.popleft()
q.append(8)
print(list(q))
Show answer

Answer: D. [6, 7, 8]

7 joins the rear, 5 leaves from the front, then 8 joins the rear.

Question 5 · multiple choice

What is the main advantage of a linked list over an array?

Show answer

Answer: B. Inserting or deleting at a known position needs no shifting of other items

Only references change; an array must shift items. Arrays, however, give faster indexed access.

Question 6 · short answer

Explain why collections.deque is preferred over a list for a queue.

Show worked answer

A queue removes items from the front. With a list, pop(0) removes the first item and then shifts every remaining item one place to the left, so it takes time proportional to the length of the list (O(n)). A deque is designed for fast operations at both ends: append() and popleft() each take constant time (O(1)), however many items the queue holds.

↑ ↓ move↵ openTab switch type