Question on Stack using Structure given below :
//Question on Stack using Structure
#include<stdio.h>
struct STACK_STRUCT {
int STACK[10];
int TOP,SIZE;
};
int isFull(struct STACK_STRUCT *STACK_POINTER){
if((*STACK_POINTER).TOP == (*STACK_POINTER).SIZE - 1)
return 1;
else
return 0;
}
int isEmpty(struct STACK_STRUCT *STACK_POINTER){
if((*STACK_POINTER).TOP == -1)
return 1;
else
return 0;
}
void push(struct STACK_STRUCT *STACK_POINTER,int data){
if(!(isFull(STACK_POINTER)))
{
(*STACK_POINTER).TOP ++;
(STACK_POINTER)->STACK[(STACK_POINTER)->TOP] = data;
}
else
printf("Stack is Full");
}
void pop(struct STACK_STRUCT *STACK_POINTER){
int t;
if(!(isEmpty(STACK_POINTER))){
t = (STACK_POINTER)->STACK[(*STACK_POINTER).TOP];
(*STACK_POINTER).TOP --;
printf("%d Poped from Stack",t);
}
else
printf("Stack is Empty");
}
int main(){
struct STACK_STRUCT STACKOBJ;
STACKOBJ.TOP =-1;
STACKOBJ.SIZE =10;
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(&STACKOBJ,data);
break;
case 2:
pop(&STACKOBJ);
break;
case 3:
if (STACKOBJ.TOP != -1)
{
printf("Elements in the Stack are : ");
for(i=0;i<=STACKOBJ.TOP;i++){
printf("%d ",STACKOBJ.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 :34
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 1
Enter the Data which you want to push :54
1. Push
2. Pop
3. Display
0. Exit
Enter the Choice : 3
Elements in the Stack are : 34 54