C program to PUSH,POP,SHOW and REVERSE a STACK
#define MAX 10 #include #include void push(int * stack,int*top,int no) { if(*top==MAX-1) { printf("\n OVERFLOW"); return; } stack[++(*top)]=no; } int pop(int *stack,int*top) { if(*top==-1) { printf("\n UNDERFLOW"); return -999; } return stack[(*top)--]; } void show(int * stack,int *top) { int i; if(*top==-1) { printf("\n stack Empty"); return; } for(i=*top;i>=0;i--) { printf("\n %d",stack[i]); } }
void reverse(int *stack,int *top) { int S2[MAX],t2=-1; int S3[MAX],t3=-1; while(*top!=-1) push(S2,&t2,pop(stack,top)); while(t2!=-1) push(S3,&t3,pop(S2,&t2)); while(t3!=-1) push(stack,top,pop(S3,&t3)); }
void main() { int S1[MAX],t1=-1; int ch,val; do { printf("\n 1 PUSH"); printf("\n 2 POP"); printf("\n 3 SHOW"); printf("\n 4 Reverse"); printf("\n 0 EXIT");
printf("\nENter your CHoice"); scanf("%d",&ch); switch(ch) { case 1:printf("\n enter Number"); scanf("%d",&val); push(S1,&t1,val); break; case 2: val=pop(S1,&t1); if(val!=-999) printf("%d popped",val); break; case 3: show(S1,&t1); break; case 4: reverse(S1,&t1); break; case 0:break; default:printf("\n Invalid choice"); } }while(ch!=0); getch(); }
AttachmentsSTACK (2541-30168-STACK.CPP)
|
No feedbacks found. Be the first to respond and make money from revenue sharing program.
|