Simulate a queue with two stacks
Stack1,stack2
First put the data into the STACK1, if you need to output data, delete the data from the Stack2, if the Stack2 is empty, the STACK1 data import Stack2
<span style= "FONT-SIZE:14PX;" > #include "static.h" #include <iostream> #include <stack>template<typename t> class Cquue{public: Cquue () {};~cquue () {};void appendtail (const T &node); T deletehead ();p rivate:stack<t> m_stack1;stack<t> M_stack2; < deposit data into the STACK1, take the data out of the Stack2, if Stack2 is empty, stack1 data into Stack2};template<typename t>void cquue< T>::appendtail (const T &node) {M_stack1.push (node);} Template<typename t> T cquue<t>::d Eletehead () {if (M_stack2.empty ()) {if (M_stack1.empty ()) {printf ("There is no data in stack "); exit (1);} while (!m_stack1.empty ()) {M_stack2.push (M_stack1.top ()); M_stack1.pop ();}} T valuetemp = M_stack2.top (), M_stack2.pop ();p rintf ("%d", valuetemp); return valuetemp;} int main () {cquue<int> cqueue;cqueue.appendtail (1); Cqueue.appendtail (3); Cqueue.deletehead (); Cqueue.appendtail (5); Cqueue.appendtail (7); Cqueue.deletehead (); Cqueue.deletehead (); Cqueue.deletehead (); return 0;} </span>
Two queue simulation stacks
The method used here is not the same as the method mentioned in the book, but the general meaning is the same, but the method in the book will reduce the judgment sentence.
Two queues, each time the data is put into an empty queue, while the data in the other queue is copied into the current queue, but also maintain an empty queue
Output data from a non-empty queue
But such a method requires a copy once for each data input, but also more troublesome
In the book, each time you output data, you need to copy the previous data into another queue.
<span style= "FONT-SIZE:14PX;" > #include "static.h" #include <iostream> #include <queue>template<typename t>class cstack{public : Cstack () {};~cstack () {};void appendtail (const t& node); T deletehead ();p rivate:queue<t> m_queue1;queue<t> m_queue2; < deposit data into the m_queue1, and then import the data into the queue2, and then each time to add data into an empty queue};template<typename t>void cstack<t >::appendtail (const t& node) {if (m_queue1.size () = = 0) {m_queue1.push (node); while (!m_queue2.empty ()) {m_ Queue1.push (M_queue2.front ()); M_queue2.pop ();}} Else{m_queue2.push (node), while (!m_queue1.empty ()) {M_queue2.push (M_queue1.front ()); M_queue1.pop ();}}} Template<typename t>t cstack<t>::d eletehead () {T tempvalue;if (m_queue1.empty () && M_queue2.empty ()) {printf ("The Stack is empty"); exit (1);} if (M_queue1.size () >0) {tempvalue = M_queue1.front (); M_queue1.pop ();} Else{tempvalue = M_queue2.front (); M_queue2.pop ();} printf ("%d", tempvalue); return tempvalue;} int main () {cstack<int> CSTack;cstack.appendtail (1); Cstack.deletehead (); Cstack.appendtail (3); Cstack.appendtail (5); Cstack.appendtail (7); Cstack.deletehead (); Cstack.deletehead (); Cstack.deletehead (); return 0;} </span>
72 stack simulation queues, two queue simulation stacks