#include<stdio.h>
#define MAX 10
void push(int stack[MAX],int *top,int val);
void pop(int stack[MAX],int *top,int *val);
void tra(int stack[MAX],int *top);
main()
{
int stack[MAX],top=-1,n,val,ch;
while(1)
{
printf("\n..............MENU..............\n");
printf("\nselect your choice\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.traverse\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element to be pushed:");
scanf("%d",&val);
push(stack,&top,val);
break;
case 2:
pop(stack,&top,&val);
break;
case 3:
tra(stack,&top);
break;
case 4:
exit(1);
default :
printf("invalid choice\n");
}
}}
void push(int stack[MAX],int *top,int val)
{
if(*top<MAX)
{
*top=*top+1;
stack[*top]=val;
}
else
{
printf("the stack is full can not push a value\n");
return;
}
}
void pop(int stack[MAX],int *top,int *val)
{
if(*top>=0)
{
*val=stack[*top];
*top=*top-1;
printf("the value poped is %d",*val);
}
else
{
printf("the stack is empty \n");
return;
}
}
void tra(int stack[MAX],int *top)
{
int i;
if(*top==-1)
{
printf("the stack is empty\n");
return;
}
else
{
printf("the elements in the stack are\n");
for(i=*top;i>=0;i--)
printf("%d\t",stack[i]);
}
}
OUTPUT
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:2
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:3
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:3
the elements in the stack are
3 2 1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 3
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 2
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the stack is empty
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:4
Process returned 1 (0x1) execution time : 32.152 s
Press any key to continue.
#define MAX 10
void push(int stack[MAX],int *top,int val);
void pop(int stack[MAX],int *top,int *val);
void tra(int stack[MAX],int *top);
main()
{
int stack[MAX],top=-1,n,val,ch;
while(1)
{
printf("\n..............MENU..............\n");
printf("\nselect your choice\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.traverse\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element to be pushed:");
scanf("%d",&val);
push(stack,&top,val);
break;
case 2:
pop(stack,&top,&val);
break;
case 3:
tra(stack,&top);
break;
case 4:
exit(1);
default :
printf("invalid choice\n");
}
}}
void push(int stack[MAX],int *top,int val)
{
if(*top<MAX)
{
*top=*top+1;
stack[*top]=val;
}
else
{
printf("the stack is full can not push a value\n");
return;
}
}
void pop(int stack[MAX],int *top,int *val)
{
if(*top>=0)
{
*val=stack[*top];
*top=*top-1;
printf("the value poped is %d",*val);
}
else
{
printf("the stack is empty \n");
return;
}
}
void tra(int stack[MAX],int *top)
{
int i;
if(*top==-1)
{
printf("the stack is empty\n");
return;
}
else
{
printf("the elements in the stack are\n");
for(i=*top;i>=0;i--)
printf("%d\t",stack[i]);
}
}
OUTPUT
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:2
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:1
enter the element to be pushed:3
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:3
the elements in the stack are
3 2 1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 3
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 2
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the value poped is 1
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:2
the stack is empty
..............MENU..............
select your choice
1.push
2.pop
3.traverse
4.exit
enter your choice:4
Process returned 1 (0x1) execution time : 32.152 s
Press any key to continue.
No comments:
Post a Comment