Exam: Use two stacks to implement the queue, and use the exam to implement the queue
# Include <iostream >#include <stack> using namespace std; template <typename T> class QUEUE {public: QUEUE (){}~ QUEUE () {} void APPEND (const T val) {while (st2.empty () = false) {// we only use st2 to save the data, and st1 serves as the intermediate exchange bridge. // First, store the data in st2 in reverse order in st1. St1.push (st2.top (); st2.pop () ;}// place the input value on the top of st1. St1.push (val); while (st1.empty () = false) {// put all the data in st1 in st2 at the moment, // It just ensures that the new val is at the bottom and achieves the idea of advanced queues. St2.push (st1.top (); st1.pop () ;}} int DELHED () {int temp = st2.top (); st2.pop (); return temp;} void Printf () {stack <int> st3 = st2; // if it is printed, a temporary variable is selected to avoid affecting the original stack. While (st3.empty () = false) {cout <st3.top () <""; st3.pop () ;}cout <endl;} private: stack <T> st1; stack <T> st2 ;}; int main () {QUEUE <int> qe; for (int I = 0; I <10; I ++) {qe. APPEND (I);} qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); qe. DELHED (); qe. printf (); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.