How To Implement A Stack Using Linked List in C?

How to implement a stack using linked list in C?

Stacks are fundamental data structures that play a crucial role in various programming applications and many students find Stack implementation quite challenging. 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?

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 a 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 a 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");

ย  ย  }

ย  ย  else

ย  ย  {

ย  ย  ย  ย  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;

ย  ย  ย  ย  }else{

ย  ย  ย  ย  ย  ย  ย  ย  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;

ย  ย  ย  ย  free(temp);

ย  ย  ย  ย  top = NULL;

ย  ย  }else{

ย  ย  ย  ย  ย  ย  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;

ย  ย  ย  ย  ย  ย  free(temp);

ย  ย  }

ย  ย  return remove;

}

void display(){

ย  ย  struct Node *temp = top;

ย  ย  if (temp == NULL)

ย  ย  {

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

ย  ย  }

ย  ย  else

ย  ย  {

ย  ย  ย  ย  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);

ย  ย  ย  ย  ย  ย  else

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

ย  ย  ย  ย  ย  ย  temp = temp->next;

ย  ย  ย  ย  }

ย  ย  ย  ย  printf("\n");

ย  ย  }

}

int main()

{

ย  ย  int element, position, choice, searchFor, del;

ย  ย  printf("\t\t STACK IMPLEMENTATION USING LINKED LIST \n");

ย  ย  do

ย  ย  {

ย  ย  ย  ย  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);

ย  ย  ย  ย  ย  ย  push(element);

ย  ย  ย  ย  ย  ย  break;

ย  ย  ย  ย  case 2:

ย  ย  ย  ย  ย  ย  printf("Pop \n");

ย  ย  ย  ย  ย  ย  del = pop();

ย  ย  ย  ย  ย  ย  if (del == INT_MIN)

ย  ย  ย  ย  ย  ย  {

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

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  ย  ย  else

ย  ย  ย  ย  ย  ย  {

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

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  ย  ย  break;

ย  ย  ย  ย  case 3:

ย  ย  ย  ย  ย  ย  display();

ย  ย  ย  ย  ย  ย  break;

ย  ย  ย  ย  case 4:

ย  ย  ย  ย  ย  ย  exit(0);

ย  ย  ย  ย  default:

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

ย  ย  ย  ย  ย  ย  break;

ย  ย  ย  ย  }

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

ย  ย  ย  ย  getch();

ย  ย  } while (1);

ย  ย  return 0;

}

In this program, we have implemented a stack called ‘Node’ of the structure data type using a 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 a 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.

ย 

Output:

ย 

Output

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.

Output

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. Also, if you are curious about Inline Function then you can read our article here.

Leave a Comment

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