Question on stack using local variable are given below :
// question on stack using local variable
#include<stdio.h>
int isFull(int TOP,int SIZE){
if(TOP == SIZE - 1)
return 1;
else
return 0;
}
int isEmpty(int TOP){
if(TOP == -1)
return 1;
else
return 0;
}
void push(int *STACK,int *TOP,int data,int SIZE){
if(!(isFull(*TOP,SIZE)))
{
(*TOP) ++;
STACK[(*TOP)] = data;
}
else
printf("Stack is Full");
}
void pop(int *STACK,int *TOP){
int t;
if(!(isEmpty(*TOP))){
t = *(STACK+(*TOP));
(*TOP) --;
printf("%d Poped from Stack",t);
}
else
printf("Stack is Empty");
}
int main(){
int SIZE = 10;
int STACK[10];
int TOP = -1;
int choice,i,data;
do{
printf("1. Push \n2. Pop\n3. Display \n0. Exit");
printf("\nEnter the Choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the Data which you want to push :");
scanf("%d",&data);
push(&STACK[0],&TOP,data,SIZE);
break;
case 2:
pop(&STACK[0],&TOP);
break;
case 3:
if (TOP != -1)
{
printf("Elements in the Stack are : ");
for(i=0;i<=TOP;i++){
printf("%d ",STACK[i]);
}
}
else{
printf("No elements present in the STACK");
}
break;
case 0:
printf("Exiting ... ");
break;
default:
printf("WRONG CHOICE !!! Enter Again. \n");
break;
}
printf("\n\n");
}while(choice != 0);
return 0;
}
Output
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 1
Enter the Data which you want to push :22
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 1
Enter the Data which you want to push :43
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 3
Elements in the Stack are : 22 43
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 2
43 Poped from Stack
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 0
Exiting ...