コード例 #1
0
void hanoi(int n, stack& current, stack& dest, stack& buffer) {
	// Move plates 1 till n-1 from current to buffer, using dest as buffer
	
	if (n > 1)
		hanoi(n - 1, current, buffer, dest);

	// Move plate n from current to dest
	int v = current.peek();
	current.pop();
	dest.push(v);

	// Move plates 1 till n-1 from buffer to dest, using current as buffer
	
	if (n > 1)
		hanoi(n - 1, buffer, dest, current);
}
コード例 #2
0
//a function that i used to minimize the length of the main function
//this function will ask the user to pick a card from the second deck.
//It will also ask for the answer and if the user answered correctly
//then they will gain a point and the card will be discard.
void output(journal_entry & to_move, stack & single, int & score, char * answer2, char answer[])
{
        if(single.peek(to_move))	//peek a card on the deck and return the answer as agruments
        {
                 answer2 = to_move.display();	//print out the question and catch the answer
                 single.pop();	//remove the card from the deck
                 cin.get(answer,SIZE,'\n');      cin.ignore(SIZE,'\n');	//ask the user for the answer
                 compare(answer, answer2, score);	//check to see if the user answered correctly
        }
        else
        {
                 cout << "Deck 2 is empty!" << endl;	//else if the deck is empty
		 return;
        }

}
コード例 #3
0
ファイル: tbt.cpp プロジェクト: agrimaseth/Data-Structures
void tbt ::create_exp()
{
    int i=0;
    cout<<"enter the postfix expression";
    cin>>post;
    char token;
    while(post[i]!='\0')
    {
        token=post[i];
        node *root=new node;
        root->data=token;
        if(isalpha(token))
        {
            stack1.push(root);
        }
        else
                {
                    node *t1=stack1.peek();
                    stack1.pop();
                    node *t2=stack1.peek();
                    stack1.pop();
                    if(t1->left==NULL&&t2->left==NULL)
                    {
                        root->left=t2;
                        root->right=t1;
                        root->lbit=root->rbit=1;

                        t2->right=root;
                        t1->left=root;
                        t1->lbit=t1->rbit=0;
                        t2->lbit=t2->rbit=0;

                        stack1.push(root);

                    }
                    else if(t1->left==NULL&&t2->left!=NULL)
                    {
                        root->left=t2;
                        root->right=t1;
                        root->lbit=root->rbit=1;

                        t1->left=root;

                        node*suc2 =new node;
                        suc2=t2;
                        while(suc2->right!=NULL)
                        {
                            suc2=suc2->right;
                        }
                        suc2->right=root;
                        stack1.push(root);


                    }
                    else if(t1->left!=NULL&&t2->left==NULL)
                    {
                        root->left=t2;
                        root->right=t1;
                        root->lbit=root->rbit=1;

                        t2->right=root;

                        node *pre1=new node;
                        pre1=t1;

                        while(pre1->left!=NULL)
                        {
                            pre1=pre1->left;
                        }

                        pre1->left=root;
                        stack1.push(root);

                    }
                    else if(t1->left!=NULL&&t2->left!=NULL)
                    {
                        root->left=t2;
                                        root->right=t1;
                                        root->lbit=root->rbit=1;

                        node*pre1=new node;
                        pre1 =t1;
                        while(pre1->left!=NULL)
                        {
                            pre1=pre1->left;
                        }
                        pre1->left=root;

                        node *suc2=new node;
                        suc2=t2;
                        while(suc2->right!=NULL)
                        {
                            suc2=suc2->right;
                        }
                        suc2->right=root;

                        stack1.push(root);



                    }
                }

                i++;
            }
            node *temp=new node;
            temp=stack1.peek();
            head->left=temp;
            head->right=head;
            head->lbit=head->rbit=1;
            while(temp->left!=NULL)
            {
                temp=temp->left;
            }
            temp->left=head;
            temp=stack1.peek();
                while(temp->right!=NULL)
                    temp=temp->right;
            temp->right=head;
            stack1.pop();
}