Friends.,hope this will help you to implement stack using linked list....!
PROGRAM CODING :
//Program created by Gowrimani...
//Stack implementation using linked list..
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node *top=NULL,*temp;
int main()
{
int choice,data;
while(1)//infinite loop is used to insert/delete infinite number of nodes
{
printf("****STACK IMPLEMENTATION USING LINKED LIST****\n");
printf("\nYour menu is\n 1.Push\n 2.Pop\n 3.Display\n 4.Exit\n");
printf("\nEnter ur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter a node data :");
scanf("%d",&data);
temp->data=data;
temp->link=top;
top=temp;
break;
case 2:
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
break;
case 3:
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
while(temp!=NULL)
{
printf(" -> %d -> ",temp->data);
temp=temp->link;
}
break;
case 4:
exit(0);
default:
printf("Invalid choice..!choose from the given option..!\n");
break;
}
}
}
No comments:
Post a Comment