C program to pop bottom element of stack;to count number of occurrences of a element in stack;to delete all the occurrences of a particular element;to delete a element by its position in 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]); } }
int popBottomElement(int *stack,int*top ) { int val; int S2[MAX],t2=-1; while(*top!=0) push(S2,&t2,pop(stack,top)); val=stack[*top]; *top=-1;
while(t2!=-1) push(stack,top,pop(S2,&t2)); return val;
} void count_occurences_of_no(int *stack,int*top,int no) { int non=0; int S2[MAX],t2=-1; while(*top!=-1) { push(S2,&t2,pop(stack,top)); if(S2[t2]==no) { non++; } } printf("\n %d Occured %d times",no,non); while(t2!=-1) { push(stack,top,pop(S2,&t2)); } }
void dellAllOccurofNo(int *stack,int *top,int no) { int S2[MAX],t2=-1; while(*top!=-1) { if(stack[*top]!=no) { push(S2,&t2,pop(stack,top)); } else *top=*top-1; } while(t2!=-1) push(stack,top,pop(S2,&t2)); }
void dellPosNo(int *stack,int *top,int pos) { int S2[MAX],t2=-1; while(*top!=-1) { if(*top!=pos-1) push(S2,&t2,pop(stack,top)); else *top=*top-1; } while(t2!=-1) push(stack,top,pop(S2,&t2)); }
void main() { clrscr(); int S1[MAX],t1=-1; int ch,val; do { printf("\n\n\n 1 PUSH"); printf("\n 2 POP"); printf("\n 3 SHOW"); printf("\n 4 Remove bottom Element"); printf("\n 5 number of occurences of a particular number"); printf("\n 6 Delete all the occurenses of a number from stack"); printf("\n 7 Delete a number by its positiosn"); 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: val=popBottomElement(S1,&t1); printf("\n %d popped",val); break; case 5: printf("\Enter the no"); scanf("%d",&val); count_occurences_of_no(S1,&t1,val); break; case 6: printf("Enter the number"); scanf("%d",&val); dellAllOccurofNo(S1,&t1,val); break; case 7: printf("Enter the position of number to be deleted"); scanf("%d",&val); if(val<1||val>MAX) printf("\n Please check the position....OUT of RANGE"); dellPosNo(S1,&t1,val); break; case 0:break; default:printf("\n Invalid choice"); } }while(ch!=0); getch(); }
AttachmentsSTACK (2548-11429-STACK.CPP)
|
No feedbacks found. Be the first to respond...
|