PSYCHOCODES

PsychoCodes.in

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\n",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...\n");
}

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("\n");
}

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\n",x);
	
}

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

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

int main(){
	push(20);
	push(30);
	push(10);
	pop();
	printf("Stack Content: \n");
	display();

	return 0;
}

Output:

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

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts