#include<stdio.h>
#include<malloc.h>
void disp();
void push(int data);
void pop();
struct node
{
int info;
struct node *link;
}*top=NULL;
main()
{
int ch,i,m;
while(1)
{
printf("\n..............MENU...........\n");
printf("\n1.push element\n");
printf("2.pop element\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the element:");
scanf("%d",&m);
push(m);
break;
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
exit(1);
}
}
}
void push(int data)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=data;
tmp->link=top;
top=tmp;
}
void pop()
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
if(top==NULL)
printf("stack is empty\n");
else
{
tmp=top;
printf("popped item=%d\n",tmp->info);
top=top->link;
tmp->link=NULL;
free(tmp);
}
}
void disp()
{
struct node *tmp;
tmp=top;
if(top==NULL)
printf("stack is empty\n");
else
{
printf("stack elements\n");
while(tmp!=NULL)
{
printf("%d\t",tmp->info);
tmp=tmp->link;
}
}
}
OUTPUT
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:1
enter the element:1
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:1
enter the element:2
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:1
enter the element:3
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:3
stack elements
3 2 1
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=3
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=2
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:2
popped item=1
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:2
stack is empty
..............MENU...........
1.push element
2.pop element
3.display
4.exit
enter your choice:4
Process returned 1 (0x1) execution time : 26.988 s
Press any key to continue.
No comments:
Post a Comment