push
#include #include #define max 9 int i,stk[max]={NULL},top1=-1,top2=-1,btn1=0,btn2=0; void display() { printf("\nThe stack is...\n\n"); for(i=0;i<=top2;i++) printf("%d ",stk[i]); } void push_first() { if(top1!=max) { for(i=top2+1;i>=btn2;i--) stk[i]=stk[i-1]; top2++; btn2++; top1++; printf("\nEnter the element:"); scanf("%d",&stk[top1]); } display(); } void push_second() { if(top2!=max) { top2++; printf("\nEnter the element:"); scanf("%d",&stk[top2]); } display(); } void pop_first() { if(top1!=btn1-1) { printf("\nPopped element :%d",stk[top1]); for(i=btn2-1;i<top2;i++) { stk[i]=stk[i+1]; stk[i+1]='\0'; } btn2--; top1--; } display(); } void pop_second() { if(top2!=top1) { printf("\nPopped element :%d",stk[top2]); top2--; } display(); } int main() { int ch; clrscr(); do { printf("\n\n MENU"); printf("\n1.Push first\n2.Push second\n3.Pop first\n4.Pop second\n5.Display\n6.Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:push_first(); break; case 2:push_second(); break; case 3:pop_first(); break; case 4:pop_second(); break; case 5:display();break; case 6:exit(0); default:printf("\nInvalid choice!"); } }while(ch!=6); getch(); return(0); }
top2--;
in pop_first()
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)