Exam: Use two stacks to implement the queue, and use the exam to implement the queue

Source: Internet
Author: User

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.

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.