algoritmauzmanı
Sign in
TREN
4Stack
Lesson 4.0

What is a stack?

A is a structure where elements are added and removed at one end only. There is a single rule: the last one in is the first one out. A rule this simple is useful in many places, from function calls to checking parentheses. This lesson covers what a stack is, which operations it offers and how it is built.

What you will learn
  • The LIFO order and how to spot it in everyday examples
  • The five stack operations and what each one returns
  • How a stack is built with an array or a linked list, and where it is used
5 min

1Last in, first out

Think of a pile of plates. You put a new plate on top, and when you need one you take the top plate. To reach the bottom plate you have to lift every plate above it. A stack works exactly like that. This order is called : Last In, First Out.

You use the same order on a computer every day:

  • The browser's back button: each new page goes onto a stack. When you press back you return to the page you opened last.
  • Undo: a text editor puts every change onto a stack. Ctrl+Z undoes your latest change, then the one before it.
  • Function calls: when one function calls another, the inner one finishes first, then the outer one carries on.

The end where elements go in and come out is called the top. You cannot reach an element in the middle or at the bottom directly; only the top one is visible.

2A stack is an abstract data type

A stack is defined by the operations it offers, not by how it is coded. Such a definition is called an (ADT). Code that uses a stack knows only the operations below; it does not know whether an array or a linked list sits inside.

OperationWhat it doesWhat it returns
push(x)Puts x on topNothing (in C, success: true or false)
pop()Removes the top elementThe removed element
peek()Looks at the top element without removing itThe top element
isEmpty()Is the stack empty?true or false
isFull()Is the stack full? (fixed-size stacks only)true or false

All of these take time: however many elements the stack holds, only the top is touched. Some books call top.

3Let's trace a sequence of operations

Start with an empty stack and run these operations in order. In the stack column the elements are written from bottom to top; the rightmost one is the top.

OperationStack (bottom → top)Returned value
push(3)3
push(8)3 8
push(5)3 8 5
pop()3 85
peek()3 88
push(1)3 8 1
pop()3 81
pop()38
isEmpty()3false

Look at the values the pops returned: 5, 1, 8. Each time, the element added most recently came out. Peek did not change the stack.

4Overflow and underflow

Two operations cannot always be done. Pushing onto a full stack is a stack ; it happens with a fixed-size array. Popping or peeking an empty stack is a stack . That is why isFull is checked before push, and isEmpty before pop and peek.

You also see the "stack overflow" error when a recursive function goes too deep. What overflows there is the program's .

5Two ways to build a stack

With an array: the elements sit in an array and top is the index of the top element. An empty stack has top = -1. It is fast and simple, but the capacity is fixed up front. Details in Creating an array stack.

30815234top
A stack in an array: 3, 8 and 5 were pushed, top = 2. The top is kept at the end of the array, so push and pop never shift an element.

With a linked list: each element is a node and top points to the first node. Push inserts at the head and pop deletes from the head. There is no size limit, but each node carries an extra pointer. Details in Creating a linked-list stack.

NULL5?0x2c108?0x1a903?0x1b20top
The same stack as a linked list: top points to 5, the element added last.

Both versions offer the same operations under the same names. Code that uses the stack cannot tell which one was chosen. When to pick which is covered in Array or linked list?.

6Where are stacks used?

  • The call stack: the program puts each function call on the and removes it when the function returns. This is what makes recursion work.
  • Checking parentheses: every opening bracket goes onto a stack, and each closing bracket must match the one on top. See Balanced parentheses.
  • Evaluating expressions: an expression is converted to with a stack, and the postfix expression is evaluated with another stack. See infix to postfix and postfix evaluation.
  • Undo and the back button: the last action is the first one undone.
  • Depth-first search (DFS): while moving through a graph or a maze, the points to come back to are kept on a stack.

7What this module covers

8Try it

On an empty stack you run push(4), push(7), pop(), push(2). What does peek() return now?

Show the answer

The answer is C. Pop removes 7, leaving only 4. Then 2 is pushed and sits on top. Peek returns the top element, 2.

An array stack with capacity 5 holds 5 elements. What happens if you call push(9)?

Show the answer

The answer is B. The fixed-size array has no room left. Push checks isFull first and reports an . For a stack that grows by itself you need a linked list or a dynamic array.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.