Implementation of a queue with two stacks

Source: Internet
Author: User

                           two stacks to implement a queue 1, linear table divided into: Sequential table and linked list. Stack: Inserts and deletes are allowed only on the tail (that is, the top of the stack). Queue: It is inserted at the end of the team, the team head is deleted. 2, stack Select Array (that is, the sequential table) structure (better than the selection of linked list structure): Because the selection of array structure can be size++ and size--, and high efficiency.       Chain list structure: 3, queue selection array structure: The selection of the array structure is not good, because the deletion needs to move forward as a whole, because the queue is deleted in the team header, delete the team head element, the other elements behind it will need to move the whole forward one bit. less efficient.       Queue selection list structure: Better than selecting array structure, because it is convenient to insert and delete elements. #include  <iostream> #include  <stack> #include  <assert.h>using namespace &NBSP;STD;//2, two stacks to implement a queue//idea: When two stacks are empty, only to do the queued operation, when the two stacks are not empty, the team, the queue operation can be achieved.       insta is mainly used in the stack, Outsta is mainly used for the stack//process://      : 1, When Outsta is empty, the queue operation is equivalent to Insta (including Insta empty and Insta not empty)//             2, when Insta is empty, Outsta is not empty (that is, the previous operation is out of the stack, Insta transferred to the Outsta element is not transferred back to the Insta), this time the Outsta all the elements out of the stack,//                 the stack to Insta, and then put the elements that are going into the queue to insta//        in the stack order.      ps: The Insta is not empty, Outsta is not empty, this situation will only be in the previous operation for the queue in the outbound process (Insta elements except the bottom of the stack are all transferred//                  to Outsta), Then this operation executes before Outsta is empty, after execution, Insta is empty//       out of the team: 1, when the Insta, Outsta are empty, there is no element in the queue, can not achieve the stack//             2, when Insta is not empty, Outsta is empty, Insta all the elements except the bottom of the stack, and then stacks to outsta according to the stack order, insta the stack bottom element//             3, when the Insta is empty, Outsta is not empty, the previous action is out of the stack, Insta transferred to the Outsta element is not transferred back to Insta, at this time Outsta stack top elements out of the stack can//             ps: There is no two stack is not empty, this situation only when the previous operation is in the queue, Then this operation executes before Outsta is empty, after execution,//                 stack1 is empty template<typename t>class  Twostackforqueue{public:twostackforqueue () {//Use the constructor of the stack to complete the}twostackforqueue (const twostackforqueue<t >& que) {//Call stack assignment operator overload/copy constructor Insta = que.insta;outsta = que.outsta;} ~twostackforqueue () {///There is nothing to do here, two stacks open up space by Satck release}void push (const t& t) {if  (outsta.size ()  == 0)        //indicates that the previous operation is a queued operation or the queue is now empty {Insta.push (t);} else{//first put the elements inside the Outsta in the stacking order into the stack to instawhile  (Outsta.size ()  != 0) {Insta.push (Outsta.top ()); O Utsta.pop ();} Insta.push (t);}} Void pop () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty if  (Outsta.size ()  == 0) {// Leave the InSta of the stack, directly out of the stack of its bottom element can be while  (insta.size ()  != 1) {Outsta.push (Insta.top ()); Insta.pop ();} Insta.pop ();} Note that the previous operation is out of the stack, at this point directly out of the stack Outsta stack top element can be Else{outsta.pop ();}} T& front () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty case if  (Outsta.size ()  != 0) {return outsta.top ();} else{//here do not map convenient, leaving the Insta stack bottom element does not move, directly return to Insta stack bottom element, because this makes Insta and Outsat are not empty,//and the above push and pop did not deal with this situation while  ( Insta.size ()  != 0) {Outsta.push (Insta.top ()); Insta.pop ();} Return outsta.top ();}} T& back () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty if  (Outsta.size ()  != 0) {// Here is not easy to figure, leaving the Outsta stack bottom element does not move, directly return to the Outsta stack bottom element, because this makes Insta and Outsat are not empty,//and the above push and pop did not deal with this situation while  ( Outsta.size ()  != 0) {Insta.push (Outsta.top ()); Outsta.pop ();} Return insta.top ();} Else{return outsta.top ();}} Bool empty () {if  (Insta.size ()  == 0 && outsta.size ()  == 0) { Return true;} Else{return false;}} Size_t size () {///either two stacks are empty and return 0, either one is empty and returns a Size of two stacks that are not empty return insta.size ()  >  outsta.size ()  ? insta.size ()  :  Outsta.size ();} To test correctness, write an output operator overload template<typename t>friend ostream& operator<< (ostream&  os, const twostackforqueue<t>& que);p rivate:stack<t> insta;stack< t> outsta;}; template<typename t>ostream& operator<< (ostream& os, const  Twostackforqueue<t>& que) {twostackforqueue<t> coutque (que);os <<  " queue:  ";//output  insta stack top-and-bottom elements based on queued sequence     outSta--> first element to be queued So first put the elements into the stack to Outsta, each output Outsta stack top element, and put it out of the stack, until Outsta inside no element can if  (Coutque.inSta.size ()  > 0) {while   (Coutque.inSta.size ()  > 0) {Coutque.outSta.push (Coutque.inSta.top ()); Coutque.inSta.pop ();}} while  (Coutque.outSta.size ()  > 0) {os << coutque.outsta.top ()  <<   "  "; Coutque.outSta.pop ();} os <<  "NULL"; return os;} Void test () {TwostaCkforqueue<int> que;que. Push (0); Que. Push (5); Que. Push (4); Que. Push (1); Que. Push (2); Que. Push (3); Cout << que << endl;que. Pop (); Que. Pop (); Cout << que << endl;cout << que. Front ()  << endl;cout << que. Back ()  << endl;cout << que. Empty ()  << endl;cout << que. Size ()  << endl;} Int main () {Test (); return 0;} Optimize the One//2, two stacks to achieve a queue//idea: When two stacks are empty, can only do the queued operation, when two stacks are not all empty, the team, the operation can be achieved.       insta is only used in the stack, Outsta only for the stack//process://       Queue: Regardless of whether Outsta is empty, are directly put elements into the stack to insta//      : 1, when Insta, Outsta are empty, there is no element in the queues, can not achieve the stack//             2, when Outsta is empty, insta all elements except the stack bottom element, And according to the stacking order into the stack to Outsta, the Insta stack of the bottom elements out of the stack//            3, When the Outsta is not empty, indicating that the previous operation is out of the stack, at this point the stack top element of the Outsta stack can be template<typename t>class twostackforqueue{public: Twostackforqueue () {//Insta and Outsta can be constructed using the constructor of the stack}twostackforqueue (const twostackforqueue& que) { Insta = que.insta;outsta = que.outsta;} ~twostackforqueue () {//insta and outsta space are freed by the stack class to complete}void push (const t& t) {Insta.push (T);} Void pop () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty if  (Outsta.size ()  == 0) {// Description at this time is the first execution of the column or before the data inside the Outsta is all out of stack while  (Insta.size ()  != 1) {Outsta.push (Insta.top ()); Insta.pop () ;} Insta.pop ();} The element that was previously left in Outsta is not all out of the stack Else{outsta.pop ();}} T& front () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty if  (Outsta.size ()  != 0) { Return outsta.top ();} else{//when Outsta is empty, it indicates that the first element of the team is the stack-bottom element of the InSta, returning it to while  (Insta.size ()  != 1) {Outsta.push (Insta.top ()); Insta.pop ();} Return insta.top ();}} T& back () {assert (Insta.size ()  != 0 | |  outsta.size ()  != 0);     //queue is empty if  (Insta.size ()  != 0) { Return insta.top ();} else{//when InSta is empty, it means that the tail element is Outsta's stack-bottom element and returns it to while  (Outsta.size ()  != 1) {Insta.push (Outsta.top ()); O Utsta.pop ();} Return outsta.top ();}} Bool empty () {if  (Insta.size ()  == 0 && outsta.size ()  == 0) { Return true;} Else{return false;}} Size_t size () {//returns the number of elements of two stacks and return insta.size ()  + outsta.size ();} To test correctness, write an output operator overload template<typename t>friend ostream& operator<< (ostream&  os, const twostackforqueue<t>& que);p rivate:stack<t> insta;stack< t> outsta;}; template<typename t>ostream& operator<< (ostream& os, const  Twostackforqueue<t>&amP; que) {twostackforqueue<t> coutque (que);os <<  "queue: ";//based on queued sequence output   Insta not empty: Insta stack top--and last queued element    outsta not empty: Outsta stack top--and first queued element while  (Coutque.outSta.size ( ) ( > 0) {os << coutque.outsta.top ()  <<  "  "; Coutque.outSta.pop ();} while  (Coutque.inSta.size ()  > 0) {Coutque.outSta.push (Coutque.inSta.top ()); Coutque.inSta.pop ( );} while  (Coutque.outSta.size ()  > 0) {os << coutque.outsta.top ()  <<   "  "; Coutque.outSta.pop ();} os <<  "NULL"; return os;} Void test () {Twostackforqueue<int> que;que. Push (0); Que. Push (5); Que. Push (4); Que. Push (1); Que. Push (2); Que. Push (3); Cout << que << endl;que. Pop (); Que. Pop (); Cout << que << endl;cout << que. Front ()  << endl;cout << que. Back () &NBSP;&LT;< endl;cout << que. Empty ()  << endl;cout << que. Size ()  << endl;} Int main () {Test (); return 0;}

This article is from the "10911544" blog, please be sure to keep this source http://10921544.blog.51cto.com/10911544/1773535

Implementation of a queue with two stacks

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: info-contact@alibabacloud.com 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.