How to implement a stack using linked list in C?

How To Implement A Stack Using Linked List in C?

Have you heard about data structures? I am sure you have heard of it if you are studying Computer Science in your School or College. So, today we are going to learn about ‘how to implement a stack using Linked List in C’ in a very simplified way.

In this post, we will be looking a little into what is a stack and what operations we can perform on it. Still, if you want to learn C programming concepts from expert C tutors at CodingZap then you can always reach out to us.

Before we get into the actual question of ‘How to implement a stack using a linked list in C?’ Let us look into what stacks and linked lists are for better understanding. Still, if you are a newbie in C programming then you can learn how to start C in 5 easy steps.

So, without any further delay, let’s jump into the topic.


What Is A Stack? Let’s Know About This First


A stack is a linear data structure that follows the ‘Last In First Out’ order to perform its operations. The Last In First Out order is often termed as LIFO.

This means that the last element which is inserted in the stack will be the first element to be removed from it.

Let us think about it using an example. Imagine that you are making a stack of coins in front of you by placing some coins one over the other.

Now, the coin which is on top of the stack will be the first one to be removed from the stack. Also, this coin is also the last coin which was the last coin added to the stack.

Hence, it follows the Last In First Out order. There are four basic operations of the stack.

Let us know about these 4 operations to get to know how to implement a stack using a linked list in C.


What are the 4 basic operations of the Stack?


The 4 basic operations of the stack are as follows:

  1. Push: The push operation in Stack is used to insert or add an element in the stack.
  2. Pop: The pop operation in Stack is used to remove or delete an element from the stack.
  3. isFull: The isFull operation in Stack checks if the stack is full or not. When the stack is full, no more elements can be pushed into it, hence it results in Stack Overflow.
  4. isEmpty: The isEmpty operation in Stack checks if the stack is empty or not. When the stack is empty, no more elements can be popped from it, hence it results in Stack Underflow.

Now, let us look into what is a linked list.


What is a Linked List?


Just like a stack, a linked list is also a linear data structure. The elements in a linked list are connected or linked with each other with the help of pointers.

Each element of a linked list is called a ‘Node’.

Each node consists of two fields, one is for storing the data and the other is to store a link or a reference to the node which comes next in the list.

Linked lists are of various types: Singly linked lists, doubly linked lists, and circular linked lists.


How to implement a stack using a linked list in C?


How to implement a stack using linked list in C?

In this post, to look at how to implement a stack using linked list in C, we will be use a singly linked list.

A singly linked list, as the name suggests can be traversed in one direction only. Hence, it is linked in a single direction.

Let us now look at the code for understanding how to implement a stack using linked list in C. If you also want to learn about dynamic memory allocation in C then you should read it here.


Code to implement a stack using linked list


#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <limits.h>

struct Node


    int data;          // for storing the elements

    struct Node *next; // pointer of the last node


struct Node *top = NULL; // points to the first node of the linked list

// Push

void push(int element)


    struct Node *newNode = top;

    if (newNode == NULL)


        newNode = (struct Node *)malloc(sizeof(struct Node));

        newNode->data = element;

        newNode->next = NULL;

        top = newNode; // top will point to the recently created node (newNode)

        printf("Element inserted! \n");




        while (newNode->next != NULL)

        { // traversing through the linked list

            newNode = newNode->next;


        // when newNode->next will be equal to NULL, we will come out of the while loop

        struct Node *lastNode = (struct Node *)malloc(sizeof(struct Node));

        newNode->next = lastNode;

        lastNode->data = element;

        lastNode->next = NULL;

        printf("Element inserted! \n");



// isEmpty() function to check if the stack is empty

int isEmpty(){

        if(top == NULL){

            return 1;


                return 0;



// Pop

int pop(){

    struct Node *temp = top;

    int remove;

    if(isEmpty() == 1){

        return INT_MIN;

    }else if(temp->next == NULL){

        remove = top->data;


        top = NULL;


            struct Node *prevNode;

            while(temp->next != NULL){

                    prevNode = temp;

                    temp = temp->next;


            // when we come out of the loop, temp will point to the last node

            remove = temp->data;

            prevNode->next = NULL;



    return remove;


void display(){

    struct Node *temp = top;

    if (temp == NULL)


        printf("Stack is empty !! \n");




        printf("Elements present in the list are : \n");

        while (temp != NULL)

        { // temp will be equal to NULL when we reach the end of the linked list

            if (temp->next != NULL)

                printf("%d ---> ", temp->data);


                printf("%d ---> NULL", temp->data);

            temp = temp->next;





int main()


    int element, position, choice, searchFor, del;




        printf("\t OPERATIONS \n");

        printf("1. Push \n");

        printf("2. Pop \n");

        printf("3. Display Elements \n");

        printf("4. Exit \n");

        printf("Enter your choice : ");

        scanf("%d", &choice);

        switch (choice)


        case 1:

            printf("Push \n");

            printf("Enter the element you want to insert : ");

            scanf("%d", &element);



        case 2:

            printf("Pop \n");

            del = pop();

            if (del == INT_MIN)


                printf("Stack is empty!! \n");




                printf("%d removed from the list. \n", del);



        case 3:



        case 4:



            printf("Wrong choice entered!! Please enter the correct choice. \n");



        printf("\nPress enter to continue \n\n");


    } while (1);

    return 0;


In this program, we have implemented a stack called ‘Node’ of the structure data type using linked list.

Node has two data members, ‘data’ for storing the elements and ‘next’ which is the reference to the last node in the stack which we have implemented using linked list.

In the main() function, we have used a switch case to perform operations like push and pop in the stack.

Also, we have defined push(), pop(), isEmpty(), and display() functions to perform the operations on the stack.

Let us look at how the stack is implemented by executing the program.





We start by performing the push operation on the stack by inserting 2 elements (23 and 34) into it.

To confirm if the elements are indeed present, we display the elements in the stack. Here, we can see that the elements are linked in a single direction.

The topmost element or the last element in the stack is 34. If you are getting errors while implementing stack using a linked list then you can read out post on Errors in C Programming and learn more about tackling errors in C.


After this, we perform the pop operation on the stack.

Upon displaying the elements present in the stack, we see that the element which is popped out of the stack is 34 which was the last element.

Hence, we see that the stack is implemented using a linked list and follows the LIFO order.

Leave a Comment

Your email address will not be published. Required fields are marked *