All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Stacks: a fixed-length list with push and pop

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

A stack is a last in, first out (LIFO) structure. The specification asks you to implement one in a fixed-length list with Create, Push, Pop, IsEmpty, IsFull and Size.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

How a stack works (spec 8.2.5)

Items are added (pushed) and removed (popped) at the same end, the top. The last item pushed is the first popped. Uses: undo, back buttons, reversing data, tracking subprogram calls.

A fixed-length implementation

Create a list of MAX_SIZE empty slots and a variable top holding the index of the top item, starting at -1 (empty).

MAX_SIZE = 4
stack = [None] * MAX_SIZE
top = -1

def is_empty():
    return top == -1

def is_full():
    return top == MAX_SIZE - 1

def size():
    return top + 1

def push(item):
    global top
    if is_full():
        print("Stack overflow")
    else:
        top = top + 1
        stack[top] = item

def pop():
    global top
    if is_empty():
        print("Stack underflow")
        return None
    item = stack[top]
    top = top - 1
    return item

push("A")
push("B")
push("C")
print(pop(), size())

Output:

C 2

Overflow and underflow

Pushing onto a full stack is overflow; popping from an empty one is underflow. Always check is_full() before a push and is_empty() before a pop. Popping does not have to erase the slot: moving top down is enough, because the slot will be overwritten by the next push.

Why global here?

The functions change top, which lives outside them, so they declare global top. The PLS allows global but says to keep it minimal; in your own designs prefer passing values in and returning them, or keep all stack code together.

Worked example

A stack of size 3 is empty. Trace: push 5, push 8, pop, push 2, push 9, push 4. What happens and what is on the stack?

  1. push 5: top 0 [5]
  2. push 8: top 1 [5, 8]
  3. pop returns 8: top 0
  4. push 2: top 1 [5, 2]
  5. push 9: top 2 [5, 2, 9], now full
  6. push 4: overflow, rejected

Answer: Stack from bottom: 5, 2, 9; push 4 causes overflow

Key idea: LIFO: push and pop at the top; top starts at -1; full when top == MAX_SIZE - 1; check for overflow before push and underflow before pop.

Check yourself

  1. What is displayed when this code runs? ```python stack = [] for ch in "CAT": stack.append(ch) result = "" while len(stack) > 0: result = result + stack.pop() print(result) ```
    Show answer

    TAC. Popping returns the items in reverse order of pushing: TAC.

  2. What is displayed when this code runs? ```python s = [] s.append(4) s.append(7) s.pop() s.append(1) s.append(9) s.pop() print(s) ```
    Show answer

    [4, 1]. push 4, push 7, pop (7), push 1, push 9, pop (9) leaves [4, 1].

  3. A fixed-length stack has MAX_SIZE = 6 and top starts at -1. After 4 pushes and 1 pop, what is the value of top?
    Show answer

    2. Each push adds 1 and each pop subtracts 1: -1 + 4 - 1 = 2.

  4. In the fixed-length stack, which condition means the stack is full?
    • top == MAX_SIZE
    • top == 0
    • top == -1
    • top == MAX_SIZE - 1
    Show answer

    top == MAX_SIZE - 1. Indexes run 0 to MAX_SIZE - 1, so the stack is full when top reaches the last index.

  5. What is stack underflow?
    • Trying to push onto a full stack
    • Trying to pop from an empty stack
    • Pushing an item of the wrong type
    • A stack that holds only one item
    Show answer

    Trying to pop from an empty stack. Underflow is removing from an empty stack; overflow is adding to a full one.

  6. This push procedure for a fixed-length stack is missing a line. After pushing 'x' and 'y', the stack list should be ['x', 'y', None]. Which line completes it? ```python MAX_SIZE = 3 stack = [None] * MAX_SIZE top = -1 def push(item): global top if top < MAX_SIZE - 1: __________ stack[top] = item push("x") push("y") print(stack) ```
    • `top = top - 1`
    • `top = MAX_SIZE`
    • `stack.append(item)`
    • `top = top + 1`
    Show answer

    `top = top + 1`. top must move up first, then the item goes into the new top slot.

  7. This code should display 9 but it does not. Which line number contains the error? ``` 1 stack = [3, 6, 9, None] 2 top = 2 3 def pop(): 4 global top 5 item = stack[top + 1] 6 top = top - 1 7 return item 8 print(pop()) ```
    Show answer

    5. top already holds the index of the top item, so line 5 should be item = stack[top].

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.