C program to Implement Stack using Linked List

A stack is a linear data structure and follows the last in first out rule, in this tutorial, we will learn how to implement stack using Linked List in C programming language.

If you are not familiar with Linked Lists we highly recommend you to read our previous articles on Linked List.

Let us first see what a stack is, a stack can be imagined as a long container and data is stored in it one by one and one on one, see the below picture to understand properly.

stack using linked list in C

We can represent stack using Linked List, for example, we have following data {2, 3, 12, 6} in our Linked List then it will be stored in a stack as shown below.

Stack using Linked List in C

Now we will write a C program to create a stack using Linked List.

Below program will consist of following functions

  1. push() — To insert an item in the stack.
  2. pop() — To remove the element from the top.
  3. empty() — to check whether the stack is empty or not
  4. display() — to display the stack.

Let’s define each of the above function and understand how each of its works.

push()

void push(int x){ struct node *temp; if(empty()){ temp = (struct node *)malloc(sizeof(struct node)); head = temp; temp->value = x; temp->addr = NULL;  } else{ y = head; temp = (struct node *)malloc(sizeof(struct node)); temp->value = x; temp->addr = y; head = temp;  } printf("Pushed --> \%d",x);  }

In the above function, we are simply moving the head pointer to the newly inserted node by assigning the head pointer to the current first nodes address part.

pop()

void pop(){ struct node *j; j = head; head = head->addr; j->addr = NULL; printf("Popped..."); }

In the pop function, we will simply assign the head pointer to the first node’s address part and will assign the address part of the popped node as NULL.

empty()

int empty(){ if(head==NULL) return 1; else return 0; }

the empty() function will check whether the head node is null or not if the head node is null then the stack is empty otherwise not.

display()

void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->value); j = j->addr; } printf(""); }

In the above function, we are traversing the Linked List until the address part of a node is found as NULL because we know the address part of the last node of a Linked List always remains NULL.

Now we will see the complete C program along with the driver function.

Stack using Linked List in C

#include<stdio.h> #include<stdlib.h>   struct node{ int value; struct node *addr;  };  struct node *y,*head;  int empty(){ if(head==NULL) return 1; else return 0; }  void push(int x){ struct node *temp; if(empty()){ temp = (struct node *)malloc(sizeof(struct node)); head = temp; temp->value = x; temp->addr = NULL;  } else{ y = head; temp = (struct node *)malloc(sizeof(struct node)); temp->value = x; temp->addr = y; head = temp;  } printf("Pushed --> \%d",x);  }  void pop(){ struct node *j; j = head; head = head->addr; j->addr = NULL; printf("Popped..."); }  void display(){ struct node *j; j = head; while(j!=NULL){ printf("\%d ",j->value); j = j->addr; } printf(""); }  int main(){ push(20); push(30); push(10); pop(); printf("Stack Content: "); display();  return 0; }

Output:

Pushed --> 20 Pushed --> 30 Pushed --> 10 Popped... Stack Content:30 20 

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий