Stack:- The Below code can shown you how to implement stack using dynamic linklist in data structure using C. We can insert the node dynamically and the Linklist works on principal Last-In-First-Out(LIFO).
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *node=NULL,*top=NULL;
void push();
void pop();
void disp();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:Push");
printf("\n2:Pop");
printf("\n3:Display");
printf("\n4:Exit");
printf("\n\tEnter Your Choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(1);
default:
printf("\n\tOption Not Available");
break;
}
}
getch();
}
void push()
{
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:-");
scanf("%d",&node->info);
if(top==NULL)
{
top=node;
node->next=NULL;
}
else
{
node->next=top;
top=node;
}
}
void pop()
{
if(top==NULL)
{
printf("\n\tUnderflow Condition No Element To Pop");
}
else
{
printf("\n\t%d is Poped ",top->info);
top=node->next;
node=top;
}
}
void disp()
{
NODE *temp;
temp=top;
if(top==NULL)
{
printf("\n\tNo Element to display Please Insert Element");
}
else
{
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->next;
}
}
}
0 Comment(s)