Stacks

Computer Science · Data structures

Question 1 of 10

0

    In a stack, which item can be read directly without removing anything?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Stacks explained

    A stack is a data structure that holds items in last-in, first-out (LIFO) order. The item added most recently is the first one removed, like a pile of plates where you take the top plate first.

    A stack has two main operations:

    • Push: add an item to the top.
    • Pop: remove and return the item at the top.

    Many stacks also have a peek operation, which reads the top item without removing it. Some courses call the operations add and remove instead of push and pop, but the idea is the same. A stack can be built with an array or a linked list, with a variable that tracks the top.

    Stacks appear in many places. Programming languages use a call stack to remember where to return to when a function finishes, and many undo features store past actions on a stack.

    A common mistake is to think a stack removes the oldest item. That is the behaviour of a queue. A stack always removes the item that was pushed last.

    Worked example

    Start with an empty stack and carry out these operations in order: push 4, push 7, push 2, pop, push 9, pop, pop. The top of the stack is on the right.

    1. Push 4, push 7 and push 2: the stack is [4, 7, 2].
    2. Pop returns 2, leaving [4, 7].
    3. Push 9 gives [4, 7, 9].
    4. Pop returns 9, leaving [4, 7]. Pop returns 7, leaving [4].
    5. The popped values, in order, are 2, 9 and 7, and the stack finally holds just 4.

    Key facts

    The practice questions on this page cover push and pop traces, reversing, bracket matching, postfix (reverse Polish) expressions, the call stack and underflow.

    • Reversing: items pushed in the order A, B, C come out as C, B, A.
    • Number of items left = pushes minus pops, as long as no pop is attempted on an empty stack.
    • Popping an empty stack is called underflow. It is an error.
    • Push and pop only touch the top, so each takes constant time, O(1)O(1).
    • In postfix the operator comes after its two operands. To evaluate, scan left to right: push numbers, and at an operator pop two values, apply it, and push the result.

    Worked example 2

    Evaluate the postfix expression 8 2 - 3 * 4 + using a stack. The top is on the right.

    1. Push 8: [8]. Push 2: [8, 2].
    2. Operator -: pop 2, then pop 8. Work out 8−2=68 - 2 = 6 and push it: [6].
    3. Push 3: [6, 3].
    4. Operator *: pop 3, then pop 6. Work out 6×3=186 \times 3 = 18 and push it: [18].
    5. Push 4: [18, 4].
    6. Operator +: pop 4, then pop 18. Work out 18+4=2218 + 4 = 22 and push it: [22].

    The answer is 22, the only item left. The value popped first is the right operand. That matters for - and /: it is 8−28 - 2, not 2−82 - 8. In normal notation this expression is (8−2)×3+4(8 - 2) \times 3 + 4.

    Worked example 3

    Are the brackets balanced in [(x+y)*{z}] and in (a+[b)]? Push each opening bracket. At a closing bracket, pop and check that it matches.

    For [(x+y)*{z}], the stack of open brackets, bottom first, changes like this:

    1. Read [ and push it. The stack is: [
    2. Read ( and push it. The stack is: [ (
    3. Read ) and pop. The popped ( matches. The stack is: [
    4. Read { and push it. The stack is: [ {
    5. Read } and pop. The popped { matches. The stack is: [
    6. Read ] and pop. The popped [ matches. The stack is empty, so the string is balanced.

    For (a+[b)]:

    1. Read ( and push it, then read [ and push it. The stack is: ( [
    2. Read ) and pop. The popped item is [, which does not match ). Not balanced.

    A string is balanced only if every closing bracket matches the top and the stack ends empty.

    Worked example 4

    A stack starts empty. Do: push 3, push 8, pop, push 6, push 1, pop, pop, pop, pop. Show the stack after each step.

    1. push 3: [3]. push 8: [3, 8].
    2. pop returns 8: [3].
    3. push 6: [3, 6]. push 1: [3, 6, 1].
    4. pop returns 1: [3, 6]. pop returns 6: [3]. pop returns 3: [].
    5. The last pop is on an empty stack, so it causes underflow.

    The popped values were 8, 1, 6 and 3. There were 4 pushes (3, 8, 6, 1) and 4 successful pops, leaving 0 items, so the fifth pop fails.

    Common mistakes

    • Popping the oldest item. That is a queue.
    • Swapping the operands in a postfix subtraction or division.
    • Forgetting to push the result back after applying an operator.
    • Declaring brackets balanced because the counts match. "([)]" has equal counts but is not balanced.
    • Counting a failed pop as removing an item.

    Exam tips

    • Draw the stack after every operation, top on one fixed side.
    • Write which operand each pop gives. The first pop is the right-hand operand.
    • For a bracket question, say what you push, what you pop and why the pair matches or fails.
    • Link to the call stack: each function call pushes a frame, and each return pops one.

    What you will practise

    101 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • A stack follows:
    • The stack operation to add an item is:
    • The operation to remove the top item is:
    • The top item without removing is:
    • Undo features often use a:
    • Checking whether brackets in an expression are balanced is commonly done with a:
    • A stack underflow happens when:
    • The call stack in a running program stores:

    …and 93 more.