Implement a stack using two queues
The declaration of this stack is as follows:
Template <typename T> class Stack_by_queue {public: Stack_by_queue (){};~ Stack_by_queue () {}; void append_tail (const T & node); T delete_head (); void Show_Stack (void); // display the output data protected from the top of the stack to the bottom of the stack in sequence: private: queue <T> queue1; queue <T> queue2 ;};
Analysis: the stack features advanced post-release. For example, for a sequence of 1, 2, 3, and 4, we try to push it into a queue1. At this time, the head of queue1 is 1 and the end is 4, at this time, if delete_head is to be implemented, the corresponding stack should be deleted 4. For the end of queue1, the first 1, 2, and 3 are not required.
The solution to implementing dalete_head is to pop up 1, 2, 3 in sequence and press it into queue2. queue1 only saves 4. In this case, if you want to delete_head, you can perform pop operations on queue1, then queue1 is blank (note this status), and I want to continue delete_head. At this time, I also press the pop-up of queue2 1 and 2 in sequence according to the above idea and press it into queue1, know the remaining element 3 at the end of the team. Just pop it out! At this time, the status is queue1 not empty, and queue2 is empty.
It is also easy to implement append_tail. Note that the two statuses after the header is deleted can be classified as one, that is, one queue is empty, and the other can be empty (the simulated stack is empty at this time ), or it is not empty. For append_tail, you only need to add the team end to the non-empty queue. If it is empty, just select one.
# Include <queue >#include <iostream> using namespace std; template <typename T> class Stack_by_queue {public: Stack_by_queue (){};~ Stack_by_queue () {}; void append_tail (const T & node); T delete_head (); void Show_Stack (void); protected: private: queue <T> queue1; queue <T> queue2 ;}; template <typename T> void Stack_by_queue <T >:: Show_Stack (void) {queue <T> Q1 (queue1 ); queue <T> Q2 (queue2); T tmp; cout <"\ n this is Show_Stack! Display Data \ n "from the top of the stack to the bottom of the stack; while (! Q1.empty () |! Q2.empty () {if (Q1.empty ()&&! Q2.empty () {// 2-> 1 if (Q2.size () <1) {return;} while (Q2.size ()! = 1) {tmp = Q2.front (); Q2.pop (); Q1.push (tmp);} cout <Q2.front () <""; Q2.pop ();} if (! Q1.empty () & Q2.empty () {// 1-> 2 if (Q1.size () <1) {return ;}while (Q1.size ()! = 1) {tmp = Q1.front (); Q1.pop (); Q2.push (tmp);} cout <Q1.front () <""; Q1.pop ();}}} // ensure that at least one empty template in the queue <typename T> T Stack_by_queue <T >:: delete_head () {T tmp; if (queue1.empty () &&! Queue2.empty () {// 2-> 1 if (queue2.size () <1) {return-1;} while (queue2.size ()! = 1) {tmp = queue2.front (); queue2.pop (); queue1.push (tmp);} tmp = queue2.front (); queue2.pop (); return tmp;} if (! Queue1.empty () & queue2.empty () {// 1-> 2 T tmp; if (queue1.size () <1) {return-1;} while (queue1.size ()! = 1) {tmp = queue1.front (); queue1.pop (); queue2.push (tmp);} tmp = queue1.front (); queue1.pop (); return tmp;} else return-1 ;} template <typename T> void Stack_by_queue <T>: append_tail (const T & node) {// ensure that a queue is empty. If it is empty, the queue is empty, if (queue1.empty ()&&! Queue2.empty () queue2.push (node); if (! Queue () & queue2.empty () queue1.push (node); if (queue1.empty () & queue2.empty () queue1.push (node); else return;} int main () {Stack_by_queue <char> my_stack; values ('A'); values ('B'); values ('C'); my_stack.Show_Stack (); my_stack.delete_head (); my_stack.Show_Stack (); my_stack.append_tail ('D'); my_stack.append_tail ('E'); my _ Stack. show_Stack (); my_stack.delete_head (); my_stack.Show_Stack (); my_stack.append_tail ('F'); my_stack.Show_Stack ();} /************************** running result: this is Show_Stack! Data is displayed from the top of the stack to the bottom of the stack c B a this is Show_Stack! Display Data a this is Show_Stack from the top of the stack to the bottom of the stack! Display Data e d a this is Show_Stack from the top of the stack to the bottom of the stack! Display Data d a this is Show_Stack from the top of the stack to the bottom of the stack! From the top of the stack to the bottom of the stack, display data f d a ************************/# include <queue> # include <iostream> using namespace std; template <typename T> class Stack_by_queue {public: Stack_by_queue (){};~ Stack_by_queue () {}; void append_tail (const T & node); T delete_head (); void Show_Stack (void); protected: private: queue <T> queue1; queue <T> queue2 ;}; template <typename T> void Stack_by_queue <T >:: Show_Stack (void) {queue <T> Q1 (queue1 ); queue <T> Q2 (queue2); T tmp; cout <"\ n this is Show_Stack! Display Data \ n "from the top of the stack to the bottom of the stack; while (! Q1.empty () |! Q2.empty () {if (Q1.empty ()&&! Q2.empty () {// 2-> 1 if (Q2.size () <1) {return;} while (Q2.size ()! = 1) {tmp = Q2.front (); Q2.pop (); Q1.push (tmp);} cout <Q2.front () <""; Q2.pop ();} if (! Q1.empty () & Q2.empty () {// 1-> 2 if (Q1.size () <1) {return ;}while (Q1.size ()! = 1) {tmp = Q1.front (); Q1.pop (); Q2.push (tmp);} cout <Q1.front () <""; Q1.pop ();}}} // ensure that at least one empty template in the queue <typename T> T Stack_by_queue <T >:: delete_head () {T tmp; if (queue1.empty () &&! Queue2.empty () {// 2-> 1 if (queue2.size () <1) {return-1;} while (queue2.size ()! = 1) {tmp = queue2.front (); queue2.pop (); queue1.push (tmp);} tmp = queue2.front (); queue2.pop (); return tmp;} if (! Queue1.empty () & queue2.empty () {// 1-> 2 T tmp; if (queue1.size () <1) {return-1;} while (queue1.size ()! = 1) {tmp = queue1.front (); queue1.pop (); queue2.push (tmp);} tmp = queue1.front (); queue1.pop (); return tmp;} else return-1 ;} template <typename T> void Stack_by_queue <T>: append_tail (const T & node) {// ensure that a queue is empty. If it is empty, the queue is empty, if (queue1.empty ()&&! Queue2.empty () queue2.push (node); if (! Queue () & queue2.empty () queue1.push (node); if (queue1.empty () & queue2.empty () queue1.push (node); else return;} int main () {Stack_by_queue <char> my_stack; values ('A'); values ('B'); values ('C'); my_stack.Show_Stack (); my_stack.delete_head (); my_stack.Show_Stack (); my_stack.append_tail ('D'); my_stack.append_tail ('E'); my_sta Ck. show_Stack (); my_stack.delete_head (); my_stack.Show_Stack (); my_stack.append_tail ('F'); my_stack.Show_Stack ();} /************************** running result: this is Show_Stack! Data is displayed from the top of the stack to the bottom of the stack c B a this is Show_Stack! Display Data a this is Show_Stack from the top of the stack to the bottom of the stack! Display Data e d a this is Show_Stack from the top of the stack to the bottom of the stack! Display Data d a this is Show_Stack from the top of the stack to the bottom of the stack! From the top of the stack to the bottom of the stack, display data f d ***************************/