Stack Implementation using Linked List – C Program

Stack Implementation using Linked List - C Program

Stack Implementation using Linked List – C Program

DATA STRUCTURES IN C – LINKED LIST (20 PROGRAMS)

Stack Implementation using Linked List – C Program :

Source Code :

#include<stdio.h>
struct node *top=NULL;
//Declared as global variable since it is accessed by multiple functions
//Pointer that points the top node of the stack

struct node
{
    	//Structure named node to store integer in variable data and a pointer variable which can store address location of next node
    	int data;
    	struct node *next;
    	//Pointer variable next is a self referential pointer which stores the address of structure node
};

void push(int data)
{
    	//This function will push a new node to the stack

    	struct node *temp = (struct node*)malloc(sizeof(struct node));
    	//malloc is used to allocate the memory dynamically
    	//Pointer variable temp is stored in heap
    	//A memory block(size of structre node) is created and a pointer variable temp is made to point to that memory location

    	temp->data=data;
    	//Store the integer data obtained from the function insert to the member variable data of newly created node 

    	temp->next=top;
    	//Make newly created node point to address location where top is pointing to

    	top=temp;
    	//Make top pointer point to newly created node
}

void pop()
{
    	//Function to delete the top node
    	if(top!=NULL)
    	{
    		//Executes if the stack has atleast one node
    		struct node *temp=top;

    		top=top->next;
    		//Making the top pointer point to next node

    		free(temp);
    		//Releasing the memory of top node
    	}
    	else
    	{
        		printf("\nStack is empty");
    	}
}

void print()
{
    	//This funtion is used to print data present in the stack 

    	struct node *temp=top;
 	   printf("\nList:");
    	while(temp!=NULL)
    	{
        		printf("\n%d ",temp->data);
        		temp=temp->next;				
    	}
}

int main()
{
    	printf("\nPushing data 1 into stack");
    	push(1);
    	print();
    	//Insert the data 1 to the stack

    	printf("\nPushing data 2 into stack");
    	push(2);
    	print();
    	//Insert the data 2 to the stack

    	printf("\nPushing data 3 into stack");
    	push(3);
    	print();
    	//Insert the data 3 to the stack

    	printf("\nPopping data from stack");
    	pop();
    	print();
    	//Delete the top element from the stack

    	printf("\nPopping data from stack");
    	pop();
    	print();
    	//Delete the top element from the stack

    	printf("\nPopping data from stack");
    	pop();
    	print();
    	//Delete the top element from the stack

    	printf("\nPopping data from stack");
    	pop();
    	//Delete the top element from the stack
    	//But there is nothing to delete now.Stack is empty

    	return 0;
}

Output :

Pushing data 1 into stack
List:
1

Pushing data 2 into stack
List:
2
1

Pushing data 3 into stack
List:
3
2
1

Popping data from stack
List:
2
1

Popping data from stack
List:
1

Popping data from stack
List:

Popping data from stack
Stack is empty

[ YOU MAY ALSO LIKE ]

Leave a Reply