1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include<stdio.h>
#include<conio.h>
void push(int*,int,int);
void pop(int*);
int empty(int);
int arr[100];
int main()
{
int n,top,choice,x,i;
char z;
printf("enter size of stack\n");
scanf("%d",&n);
top=-1;
for(i=0;;i++)
{
printf("1.push\n2.pop\n");
scanf("%d",&choice);
if(choice==1)
{
printf("enter element to be pushed\n");
scanf("%d",&x);
push(&top,x,n);
}
else if(choice==2)
pop(&top);
printf("want to stop?(y/n)\n");
scanf("%c",&z); //this line
if(z=='y')
break;
}
getch();
}
void push(int *top, int x,int n)
{
int i;
if(*top==n-1)
printf("stack overflow\n");
else
{
*top=*top+1;
arr[*top]=x;
}
printf("STACK IS:\n");
for(i=0;i<=*top;i++)
printf("%d",arr[i]);
printf("\n\n");
}
void pop(int *top)
{
int i;
if(empty(*top))
printf("stack underflow");
else
*top=*top-1;
printf("STACK IS:\n");
for(i=0;i<=*top;i++)
printf("%d",arr[i]);
printf("\n\n");
}
int empty(int top)
{
if(top==-1)
return 1;
else
return 0;
}
|