Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
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,&current);
	 current=current->right;
	}
	else
	 return;
 }
}
Ejemplo n.º 3
0
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
 }
}
Ejemplo n.º 4
0
void display()
{
    int i;
    if(stempty()) puts("Stack Underflow!");
    else for(i=st.top; i>=0; i--) printf(" %d ", st.s[i]);
}