int main() { int item, choice; char ans; st.top=-1; puts("Implementation of Stack"); do{ puts("1. Push \n2. Pop \n3. Display"); scanf(" %d%*c", &choice); switch(choice){ case 1: puts("Enter item to push:"); scanf(" %d%*c", &item); if(stfull()) puts("Stack Overflow!"); else push(item); break; case 2: if(stempty()) puts("Stack Underflow!"); else printf("Popped element: %d", pop()); break; case 3: display(); break; } puts("Continue?"); scanf(" %c%*c", &ans); } while(ans=='Y' || ans=='y'); return 0; }
void bintree<T>::preorder(node *root) { node *current,*s[10]; int top=-1; if(root==NULL) { cout<<"\n The Tree is empty"<<endl; return; } current=root; for(;;) { while(current!=NULL) { cout<<" "<<current->data; push(current,&top,s); current=current->left; } if(!stempty(top)) { pop(&top,s,¤t); current=current->right; } else return; } }
void bintree<T>::postorder(node *root) { struct stack { node *element;//Here placing the node containing value int check;//check 1 means visiting left subtree //check 0 means visiting right subtree }st[10]; int top=-1; node *current; if(root==NULL) { cout<<"\n The Tree is empty"<<endl; return; } current=root; for(;;) { while(current!=NULL) { top++; st[top].element=current; st[top].check=1;//visiting the left subbranch current=current->left; } while(st[top].check==0) { current=st[top].element; top--; cout<<" "<<current->data; if(stempty(top)) return; } current=st[top].element;//pushing the element onto the stack current=current->right; st[top].check=0;//visiting right subtree } }
void display() { int i; if(stempty()) puts("Stack Underflow!"); else for(i=st.top; i>=0; i--) printf(" %d ", st.s[i]); }