STACK OPERATION USING LINKED LIST


Program

#include <>
#include <>
#define NULL 0
struct node
{
int data;
struct node *link;
};
struct node *top,*newnode,*tempnode;
void main()
{
int ch;
clrscr();
do
{
printf(“\n\t\t STACK OPERATION USING LINKED LIST”);
printf("\n\t\t--------------------------------------------------------- ");
printf("\n 1.PUSH\n 2.POP\n 3.PEEK\n 4.DISPLAY\n 5.EXIT");
printf("\n Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}while(ch!=5);
getch();
}

push()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter The Data :");
scanf("%d",&newnode- >data);
newnode- >link=NULL;
if(top==NULL)
top=newnode;
else
{
newnode- >link=top;
top=newnode;
}
}

pop()
{
if(top==NULL)
printf("\n Stack Underflow");
else
{
printf("\n The deleted data is:%d",top- >data);
top=top- >link;
}
}

peek()
{
if(top==NULL)
printf("\n Stack Underflow");
else
printf("\n The peeked data is:%d",top- >data);
}




display()
{
if(top==NULL)
printf("\n Stack Underflow");
else
{
printf("\n The values are:");
tempnode=top;
while(tempnode!=NULL)
{
printf("\n%d",tempnode- >data);
tempnode=tempnode- >link;
}
}
}

Output

1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice: 1

Enter The Data : 2

1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice: 1

Enter The Data : 5

1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice: 3


The peeked data is: 5
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice: 2

The deleted data is: 5
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice: 4

The values are: 2
1.PUSH
2.POP
3.PEEK
4.DISPLAY
5.EXIT
Enter Your Choice:5