stack using Linked List, Stack implementation using Linked List, operation of the stack
Write a program in C language to implement a different operation of the stack using Linked List data structure.

Below is a picture of Stack implementation using Linked List for your reference:




Implementation of the above problem using C language:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int data,ch,c;
struct node
{
int element;
struct node*next;
}
*s=NULL;
void push(void);
void pop();
void display(void);
void main()
{
int ch,c,i;
printf("\n\t\t Implementation of Stack using Linked List");
while(1)
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
break;
}
}
}
void push(void)
{
struct node*temp;
temp=malloc(sizeof(struct node));
printf("\nEnter the element:");
scanf("%d",&data);
temp->element=data;
temp->next=s;
s=temp;
}
void display(void)
{
struct node*P;
P=s;
if(s==NULL)
{
printf("\nStack is empty");
}
while(P!=NULL)
{
printf("\t%d",P->element);
P=P->next;
}
}
void pop(void)
{
struct node*temp,*next,*p;
if(s==NULL)
{
printf("\nStack is empty");
return;
}
temp=s;
printf("\nThe popped element is %d",temp->element);
s=temp->next;
free(temp);
}


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput