How to Implement a Linked Stack in C
By: Scott Cosentino
Tags: Data Structures, C, C Programming, Programming

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.

Our stack after the push operation

If we wanted to then pop from this stack, it would remove the current top, and update the top to be the thing that comes next in the stack. In this case, 8 would be removed and returned to the user, and 7 would become the top of the stack.

Our stack after the pop operation

Finally, if we wanted to peek on this stack, it would simply tell us the top value, without modifying the stack at all. In this case, it would return 7, and the stack would remain the same.

When we implement a stack in C, we typically use two structs. The first struct is called a Node, and it keeps track of a single element, as well as a pointer to the next element in the stack. The second struct is called the Stack, which stores the top element as a node. Since the node has a pointer to the next element in the stack, if we ever need to get the element below the top, we can use the pointer to find it.

So, in our stack example, if 7 is the top of the stack, it will have a pointer to 6, which is the next element in the stack. The node for 6 will have a pointer to 5, since 5 comes after 6. Finally, 5 will have a pointer to NULL, since there are no values that follow it. The following code shows how these two structs can be implemented in C.

The node and stack structs

In this implementation, our node keeps track of its value using the data attribute. It additionally has a pointer attribute called next, that points to the node that directly follows it. Our stack tracks the node that is at the top through the top attribute, and also keeps track of its size in the size attribute.

When we first create a stack, we will want to initialize its attributes appropriately. To do so, we will use an initialization function, which will take in our stack struct, and set the top to NULL, and the size to 0. Doing so will ensure that when we insert our first value, it points to NULL as its next entry, rather than garbage data.

The initalization funciton

Now that our stack is complete, we will begin to look at how we can implement our push, pop, and peek operations. We will also create a method to display the data currently on the stack, to be able to verify that our operations are working correctly.

For the push operation, we will take in any integer value the user wishes to push onto the stack. We will start by creating a new node to store the data provided by the user. Once we have added the data to the node, we will set the node’s next value to the current top of the stack and set the current top of the stack equal to the node we are pushing. Once this is completed, we increase the size of the stack by 1 so that it matches the stack after the insert is completed.

The push operation

For the pop operation, our first step will be to make sure that there is at least one item on the stack that can be removed. To do this, we can look at the size of the stack to check if it is 0. If the size of the stack is not 0, we proceed to pop an item off the stack. Otherwise, we will print an error message, and end the program. When popping from the stack, we start by storing the value of the top element on the stack. We then update the top of the stack to be the next available element, which is the one that the top’s next attribute points to. Finally, we decrease the size of the stack by 1, and return the value that was at the top of the stack to the user.

The pop operation

The peek operation is basically a simplified version of the pop operation. We do the same steps, aside from updating the top and size of the stack.

The peek operation

With these operations implemented, we now have a fully functional stack. To help with testing our stack, I will implement one additional method called print, which will print the elements inside of the stack. For this function, we will create a temporary node, and set it equal to the top of the stack. From here, we will print the temporary node, then move it to the next link, until we reach the end of the stack, which is a NULL node.

The print operation

To test this implementation, we can create a main function, and run through each of our methods.

The main function

There are a few things to note in how we setup our stack to be used. First, when we declare our stack struct, we malloc for the size of the stack, since it needs to be initialized in memory before we can use it. After this, we run the init function first to setup our stack with the proper initial values. Once this is done, we can run push, pop, peek, and print operations in any sequence to see what results we get. Running this sequence, I get the following output.

The output of our stack test

The full C code for this stack implementation is available at: https://github.com/scprogramming/The-Complete-Guide-to-C-Data-Structures-and-Algorithms/tree/master/Stacks