What is a stack?

Published

2023-08-05

A stack is a data structure that operates on a LIFO basis. LIFO is short for last in, first out. We begin with an empty stack and push items onto the stack. The most recently pushed item is at the top. When we push an item onto the stack other items get pushed “further down” the stack. The only element of the stack that is accessible is the one at the top. To access it we may peek (to check its contents) or we can pop it off the stack.

Popping an item off the stack causes the items below to “rise up” and a new item (if any) will take the place of the recently popped item at the top of the stack.

Schematic diagram of a stack, showing push and pop actions.

In this module, we’ll see how to represent a stack with nodes of a linked list and we’ll implement the same in C++.

Further reading:

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.