Implement a queue with two stacks and implement stacks with two queues

Source: Internet
Author: User

Title One: implementation of the queue with two stacks, the declaration of the queue as follows, implement its two functions Appendtail and Deletehead, respectively, to complete the queue at the end of the insertion node and the head of the queue to delete the node function.

Template <class t>

Class Cqueue

Cqueue ();

~cqueue ();

void Appendtail (const t& node);

T Deletehead ();


Stack<t> Stack1;

Stack<t> Stack2;


As shown: each insertion of the node is inserted into the stack1, for example, insert A,b,c, and then in order to reach the first-in-one queue of the feature is to delete a, but stack1 only the characteristics of the stack, only the first output C, it is necessary to stack1 data sequentially output to Stack2, The Stack2 sequence is C,b,a, at which point the top of the Stack2 is a, which is the element we need to output. In other words, each time the insertion needs to be done in Stack1, the deletion operation first needs to check whether the Stack2 is empty, if not empty, then directly output stack2 the top of the stack, otherwise stack1 elements are all imported into the Stack2 and then output.

So you can write a simple code like this:

1#include <iostream>2#include <stack>3 using namespacestd;4Template <classT>5 classcqueue{6 cqueue ();7~cqueue ();8     voidAppendtail (Constt&node);9 T deletehead ();Ten Private: OneStack<t>Stack1; AStack<t>Stack2; - }; -Template <classT> the voidCqueue<t>::appendtail (Constt&node) - { -Stack1.push (node);//into the queue are all put into Stack1 - } +Template <classT> -T cqueue<t>::d eletehead () + { A     if(Stack2.size () <=0) at     { -         if(Stack1.size () <=0) -             Throwexception"The queue is empty");//two stacks are empty -         Else{//Stack1 is not empty, Stack2 is empty, then stack1 all into Stack2 -              while(Stack1.size () >0){ -T value (); in Stack2.push (value); - Stack1.pop (); to             } +         } -}//Stack2 Direct Output if not empty theT R_value (); * Stack2.pop (); $     returnR_value;Panax Notoginseng}

Topic Two: Using two queues to implement the stack, the stack is declared as follows, respectively, implement the push () function of the stack and the Topanddelete () function, to complete the top of the stack insert and delete (return stack top value) operation

Template <class t>

Class cstack{
Cstack ();

~cstack ();

void push (const t& node);

T Topanddelete ();


Queue<t> queue1;

Queue<t> queue2;


That is, according to the above analysis before each delete operation, there is always a queue is empty, a queue is full (of course, two are empty when our delete operation directly throws an exception), and then full of that queue must be we want to complete the delete operation, It is necessary to move all the elements except the tail element of the full queue to another empty queue, and then delete this unique element (there must be another queue empty after deletion). So each time you only need to detect which queue is not empty and then complete the appropriate operation.

The code is as follows:

1#include <iostream>2#include <queue>3 using namespacestd;4Template <classT>5 classcstack{6  Public:7 Cstack ();8~Cstack ();9         voidPushConstt&node);Ten T topanddelete (); One Private: AQueue<t>queue1; -Queue<t>queue2; - }; theTemplate <classT> - voidCstack<t>::p Ush (ConstT &node)//choose to insert queue1 each time - { - Queue1.push (node); + } -template<classT> +T cstack<t>:: Topanddelete () A { at     if(Queue1.empty () && queue2.empty ()) {//throws an exception if NULL -         Throwexception"The stack is empty"); -     } -     Else if(!queue1.empty () && queue2.empty ()) {//queue1 is not empty, queue2 is empty -          while(Queue1.size () >1){ -T value =Queue1.front (); in Queue2.push (value); - Queue1.pop (); to         } +T R_value =Queue1.front (); - Queue1.pop (); the         returnR_value; *     } $     Else{//queue2 is not empty, queue1 is emptyPanax Notoginseng          while(Queue2.size () >1){ -T value =Queue2.front (); the Queue1.push (value); + Queue2.pop (); A         } theT R_value =Queue2.front (); + Queue2.pop (); -         returnR_value; $     } $}

Implement a queue with two stacks and implement stacks with two queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.