/*write a menu driven program using c for Dynamic implementation
of stack the menu include:-
1:-pop
2:-push
3:-display
4:-Exit
*/
#include<stdio.h>
#include<conio.h>
int item;
struct node
{
int data;
struct node *link;
};
struct node *top=NULL;
void push();
void pop();
void display();
void main()
{
int choice;
char ch;
clrscr();
do
{
printf("\n1:push\n");
printf("\n2:pop\n");
printf("\n3:display\n");
printf("\n4:exit\n");
printf("\nenter your choice:-");
scanf("\n%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit();
default:printf("\nyou entered wrong choice");
}
printf("\ndo you want to continue(Y|N):-");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
getch();
}
void push()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\nenter the element:- ");
scanf("%d",&temp->data);
temp->link=NULL;
if(top==NULL)
top=temp;
else
{
temp->link=top;
top=temp;
}
}
void pop()
{
struct node *temp;
if(top==NULL)
{
printf("\nstack is empty");
}
else
{
temp=top;
item=top->data;
top=top->link;
free(temp);
printf("the deleted element is %d",item);
}
}
void display()
{
struct node *temp=top;
if(temp==NULL)
printf("\nstack is empty");
else
{
printf("\nthe element in stacks are:-");
while(temp!=NULL)
{
printf("\n%d",temp->data);
temp=temp->link;
}
}
}
/*
1:push
2:pop
3:display
4:exit
enter your choice:-1
enter the element:- 4
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-1
enter the element:- 7
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-1
enter the element:- 6
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-2
the deleted element is 6
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-3
the element in stacks are:-
7
4
do you want to continue(Y|N):-n
*/
/*
1:push
2:pop
3:display
4:exit
enter your choice:-8
you entered wrong choice
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-2
stack is empty
do you want to continue(Y|N):-y
1:push
2:pop
3:display
4:exit
enter your choice:-3
stack is empty
do you want to continue(Y|N):-n
*/
No comments:
Post a Comment