What is a stack?

The key idea

A stack processes the most recently added item first. This is last in, first out, or LIFO. Its central operations are push to add an item, top to inspect the latest item and pop to remove it.

The rule describes an interface and an ordering policy. The underlying storage can vary. Do not confuse the stack abstract data type with every technical use of the word stack, such as the runtime call stack.

Trace a sequence of operations

Read the contents below from oldest to newest, with the top at the right.

OperationContentsTop
Push 101010
Push 2010, 2020
Push 3010, 20, 3030
Pop10, 2020
Push 4010, 20, 4040

The next item removed is 40, not 10. A queue would follow a different ordering policy.

Use a stack in C++

#include <iostream>
#include <stack>

int main() {
    std::stack<int> history;
    history.push(10);
    history.push(20);
    while (!history.empty()) {
        std::cout << history.top() << '\n';
        history.pop();
    }
}

The output is 20 and then 10 on separate lines. The program checks that the stack is non-empty before reading its top. In std::stack, pop removes the item but does not return its value; read top first when you need the value.

For this small example the default underlying container provides the usual constant-time end operations. When you analyse another implementation, check its documented operation costs rather than assigning costs solely from the word stack.

What does an undo feature need?

Imagine a text exercise with actions “add title,” “add paragraph” and “make title bold.” Undo should first reverse the bold operation. A stack can preserve that ordering, but the application must also know how to reverse each action.

Saving only the action's name may be insufficient. You may need a previous value or enough information to restore the prior state. A redo feature introduces additional state and rules when a new edit occurs after undo.

Test the empty case

Decide what your application does when undo is requested and there is no action left. It might disable the control or report that nothing can be undone. The implementation must avoid reading a top item that does not exist.

Test one item, several items and repeated removal until empty. Write the expected contents after every operation before checking the code.

Your exercise

Push the values 3, 7 and 2. Pop twice, then push 9. The stack should contain 3 and 9, with 9 at the top. Explain the result without executing a program.

Gammal Tech's Data Structures course follows C++. Use this exercise to practise the ordering rule, then compare it with the queue guide. Recognising which item must be processed next is the first step toward choosing the right structure.

Choose your next step with Gammal Tech

Choose one skill you want to demonstrate, then review the relevant course and its prerequisites. Ask Gammal Tech about the content, study arrangements and current fees before enrolling.

References and course details

Use these references for further study. The examples and exercises in this article are educational; consult the catalogue for course details.

Articles & Guides