How to Implement a Linked Stack in C

Scott Cosentino
The Startup
Published in
6 min readAug 12, 2020

--

A stack is a set of items stored using a last-in first-out or LIFO policy. This means that the last thing that is inserted to the stack is the first thing that is removed from the stack. Often, we might think of a stack like a stack of dinner plates. When we place a plate onto the stack, we will place it on the top, and in turn when we want to take a plate off the stack, we also remove it from the top. This is the same sort of structure we wish to implement with a programming stack.

Let’s start by having a look at a visual example of a stack. Suppose I insert three values onto my stack, which are 5, 6, and 7. If they are inserted in the order specified, our stack will look like below.

A simple stack

The last thing that was inserted, which is 7, is the top item on the stack, and the first thing inserted, which is 5, is the bottom item on the stack. When we work with stacks, we typically implement three methods to manipulate the data on them. These methods are push, pop, and peek. Push will insert a new value on top of the stack, pop will remove the top value from the stack, and peek will return the value of the top of the stack.

Using our example, suppose we wanted to now push the value 8 onto the stack. When we do this, 8 becomes the new top of the stack, and the item directly below it was the previous top, 7.

--

--