- #1
Crystal037
- 167
- 7
- TL;DR Summary
- switch in c programming inside a while but argument of switch is outside the while
C:
#include<stdio.h>
#define MAX_SIZE 6
int pop(int a[]);
void push(int a[],int e);
int pal(int a[]);
int *top;
int stack[MAX_SIZE];
int main(){
int choice,done=0,ele;
printf("1.Push\t2.Pop\t3.Pal\t4.Exit\n");
printf("Enter your choice:\t");
scanf("%d",&choice);
while(!done){
switch(choice){
case 1:printf("\nEnter the element to be pushed\n");
scanf("%d",&ele);
push(stack,ele);
case 2: if(pop(stack)==-1)
printf("\nStack is empty\n");
else{
printf("\nDeleted Element=%d",pop(stack));
}
case 3:done=1;
default:printf("\nInvalid Choice\n");
}
}
}
void push(int a[],int e){
if(*top==MAX_SIZE-1){
printf("\nStack Overflow\n");
}
else{
a[++*top]=e;
}
}
int pop(int a[]){
if(*top==-1){
printf("\nStack Underflow\n");
return -1;
}
else{
return a[*top--];
}
}
Last edited by a moderator: