How it works...

First, a structure is defined, called a node, that consists of two members: one is the data and the other is a pointer called next. Because we want our stack to only store integer values, the data member of the structure is defined as an integer for storing integers and the next pointer is used to connect other nodes. Initially, the top pointer is set to NULL.

A while loop is set to execute, within which a menu is displayed. The menu is set to display three options: 1, to push into the stack; 2, to pop from the stack; and 3, to quit. Until the user enters 3 in the menu, the while loop will continue executing and keep displaying the menu, prompting the user to enter the desired option. If the user enters 1 to push a value to the stack, a new node is created by newNode. The user is prompted to enter the value to be pushed to the stack. Suppose the data that's being entered by the user is 10. Here, that value will be assigned to the data member of newNode, as follows:

Thereafter, the push function is invoked and newNode and the top pointer are passed to it. In the push function, the next pointer of newNode is set to point at the top pointer, which is NULL, and then the top pointer is set to point at X, as follows:

The top pointer must always point to the last inserted node. Due to this, it is set to point at newNode. After completing the push function, control goes back to the main function, where the menu will be displayed once more.

Let's assume that the user enters 1 to push another value to the stack. Again, a new node is created by newNode. The user is asked to enter the value to push. Assuming that the user enters 20, the value 20 will be assigned to the data member of newNode. The push function is invoked and newNode and the top pointer are passed to it. Here, the top pointer is pointing at the node that was pushed earlier, as follows:

In the push function, the next pointer of newNode is set to point at the node where the top pointer is pointing to, as follows:

Then, the top pointer is set to point at newNode, as follows:

After executing the push function, the menu will be displayed again. Let's assume that the user wants to pop a value from the stack. To do this, they will enter 2 in the menu. The pop function will be invoked and the top pointer will be passed to it. In the pop function, it's ensured that the top pointer is not NULL because if it is, this means the stack is already empty; a value can't be popped out from an empty stack. To get the value from the stack, we will use a temporary pointer called temp. The temp pointer is set to point at the node that's being pointed to by the top pointer:

Thereafter, the top pointer is set to move to the next node, that is, the node where its next pointer is pointing to. The node that's being pointed at by the temp pointer is returned to the main function:

In the main function, the node that's returned by the pop function is assigned to recNode. First, it's confirmed that recNode is not NULL. Then, the value in its data member is displayed on the screen. So, 20 will be displayed on the screen.

After executing the pop function, the menu will be displayed once more, asking the user to enter the desired option. Let's assume that the user presses 2 to pop another value from the stack. Again, the pop function will be invoked. In the pop function, we check that the top pointer is not NULL and that it's pointing to a node. Because the top pointer is pointing at a node and is not NULL, a temporary pointer, temp, is set to point at the node that's being pointed to by the top pointer:

Thereafter, the top pointer is set to point to where its next pointer is pointing. The next pointer of top is pointing at NULL, so the top pointer will be set to NULL and the node that is being pointed to by the temp pointer is returned to the main function.

In the main function, the returned node from the pop function is assigned to the recNode pointer. After confirming that recNode is not pointing at NULL, the value in the data member of recNode is displayed on the screen. So, the value 10 will appear on the screen. After executing the pop function, the menu will be displayed again on the screen.

Let's assume that the user wants to pop the stack once more. But at this point, we know that the stack is empty. When the user presses 2 on the menu, the pop function will be invoked. However, since the value of the top pointer is NULL, the pop function will return a NULL value to the main function. In the main function, the NULL value that was returned by the pop function is assigned to the recNode pointer. Because the recNode pointer is assigned NULL, a message stating stack is empty will be displayed on the screen. Again, the menu will be displayed, prompting the user to enter a choice. Upon entering 3, the program will terminate.

The program is compiled using GCC. Because no error appears on compilation, this means the stacklinkedlist.c program has successfully compiled into the stacklinkedlist.exe file. On executing the file, we get a menu, prompting us to push or pop from the stack, as shown in the following screenshot:

While popping from the stack, you must have noticed that the stack is a LIFO structure where the value that was pushed last was the first to be popped out.

Now, let's move on to the next recipe!