C + + Two stacks implement one queue and two queues to implement a stack __c++

Source: Internet
Author: User


Today in C + + implementation of the next two stack simulation of a queue and two queues to simulate a stack! Code may have many vulnerabilities, if the reader discovers the problem,

Can be timely feedback, thank you very much ...

The code is as follows:

#include <iostream> #include <stdlib.h> #include <stack> #include <queue> using namespace std;
#if 1//******* two stacks implement a queue ******** typedef int ELEMTYPE;  typedef struct {stack<elemtype>s1;  Responsible for entering queues stack<elemtype>s2;

Responsible for the queue}squeue;
   Determines whether the queue is an empty bool IsEmpty (Squeue &q) {if (Q.s1.empty ()) && (Q.s2.empty ()) {return true;
return false;

}//into queue void EnQueue (Squeue &q, Elemtype e) {Q.s1.push (e);}

Queue Size int getqueuesize (Squeue &q) {return q.s1.size () + q.s2.size ();}
        Out queue void dequeue (Squeue &q) {if (Q.s2.empty ()) {while (!q.s1.empty ()) {Q.s2.push (Q.s1.top ());
      Q.s1.pop ();  } if (!q.s2.empty ()) {//Team empty q.s2.pop ();
        Out queue}//Fetch team first element Elemtype Getfront (Squeue &q) {if (Q.s2.empty ()) {while (!q.s1.empty ()) {
        Q.s2.push (Q.s1.top ());
      Q.s1.pop ();
   } if (Q.s2.empty ()) {//team empty throw; RetUrn Q.s2.top ();
  int main () {squeue sq;
  EnQueue (Sq, 1);
  EnQueue (Sq, 2);
  EnQueue (Sq, 3);
  EnQueue (Sq, 4);
  EnQueue (Sq, 5);
  EnQueue (Sq, 6);
  cout << getfront (sq) << Endl;

  dequeue (SQ);
  cout << getfront (sq) << Endl;

  dequeue (SQ);
  cout << getfront (sq) << Endl;
  dequeue (SQ);
  cout << getfront (sq) << Endl;

  cout << getfront (sq) << Endl;
  cout << getqueuesize (sq) << Endl;
return 0;
#endif #if 1//* * * * Two queues implement a stack * * * typedef int ELEMTYPE;  typedef struct {queue<elemtype>q1;  Team queue<elemtype>q2;

Relay}qstack;
    Whether the stack is an empty bool IsEmpty (Qstack s) {if (S.q1.empty ()) {return true;
return false; }//stack size int getstacksize (Qstack s) {return s.q1.size ();}//Pressure stack void Push (Qstack &s, Elemtype e) {S.q1.push (E
);
         }//Stack void Pop (Qstack &s) {if (!s.q1.empty ()) {while (S.q1.size ()!= 1) {//queue Q1 n-1 elements moved to Q2 S.q2.push (s.q1. Front ());
       S.q1.pop ();
       } s.q1.pop ();
         while (!s.q2.empty ()) {S.q1.push (S.q2.front ());
       S.q2.pop ();  Qstack &s {if (S.q1.empty ()) {throw) on the top element of the stack;
     Stack null} while (!s.q1.empty ()) {//queue Q1 n elements move to Q2 S.q2.push (S.q1.front ());
   S.q1.pop ();  } elemtype tmp = S.q2.back ();
     Gets the nth element while (!s.q2.empty ()) {S.q1.push (S.q2.front ());
   S.q2.pop ();
return TMP;
    int main () {Qstack qs;
    Push (QS, 1);
    Push (QS, 2);
    Push (QS, 3);
    Push (QS, 4);
        try {cout << top (QS) << Endl;
        Pop (QS);
        cout << Top (QS) << Endl;
        Pop (QS);
        cout << Top (QS) << Endl;
        Pop (QS);
        cout << Top (QS) << Endl;
        Pop (QS);
    cout << "Size:" << getstacksize (QS) << Endl; catch (...)
  {} if (IsEmpty (QS)) {cout << "stack empty" << Endl;  else {cout << stack non-empty << Endl;
return 0; } #endif//1


Related Article

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.