第五章 栈与队列part01

发布时间 2023-12-09 19:04:41作者: 晴夜空

第五章 栈与队列part01

 

 

232.用栈实现队列

 

 

基础逻辑 (用于 理解 , 直接 运行 的 话 会 报错 ,C++ STL stack 定义 的 不太一样) :

注 : //C++ STL Stack 的 pop 还 不管 弹数 , 得 用 top() 拿

 

逻辑 Code :

class MyQueue {
public:
   stack<int> stack_SimulateQueue_A ;      //用 于 日常 储存 元素
   stack<int> stack_SimulateQueue_B ;


   MyQueue() {

            //用于 操作 时 的 辅助 缓存 ( "辅助 Cache)

  }
   
   void push(int x) {

       stack_SimulateQueue_A.push(x);



  }
   
   int pop() {

       int Cache_Num = -1;
                                           //int Cache_Num ;

       while(!stack_SimulateQueue_A.empty())
      {

           //C++ STL Stack 的 pop 还 不管 弹数     , 得 用 top() 拿
           //这里 储存 一下 Java 的 操作

           stack_SimulateQueue_B.push(stack_SimulateQueue_A.pop());

      }

       Cache_Num = stack_SimulateQueue_B.pop();

       while(!stack_SimulateQueue_B.empty())
      {
           stack_SimulateQueue_A.push(stack_SimulateQueue_B.pop());

      }

       return Cache_Num ;



  }
   
   int peek() {

       int Cache_Num = -1;
                                           //int Cache_Num ;

       while(!stack_SimulateQueue_A.empty())
      {
           stack_SimulateQueue_B.push(stack_SimulateQueue_A.pop());

      }

       Cache_Num = stack_SimulateQueue_B.peek();

       while(!stack_SimulateQueue_B.empty())
      {
           stack_SimulateQueue_A.push(stack_SimulateQueue_B.pop());

      }

       return Cache_Num ;


  }
   
   bool empty() {
       return stack_SimulateQueue_A.empty();
  }
};

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/

 

 

 

C++ STL Code :

class MyQueue {
public:
   stack<int> stack_SimulateQueue_A ;      //用 于 日常 储存 元素
   stack<int> stack_SimulateQueue_B ;


   MyQueue() {

            //用于 操作 时 的 辅助 缓存 ( "辅助 Cache)

  }
   
   void push(int x) {

       stack_SimulateQueue_A.push(x);



  }
   
   int pop() {

       int Cache_Num = -1;
                                           //int Cache_Num ;
       //int num_Pass = -1;
       int num_Pass ;

       while(!stack_SimulateQueue_A.empty())
      {
           num_Pass = stack_SimulateQueue_A.top();

           stack_SimulateQueue_A.pop();

           //C++ STL Stack 的 pop 还 不管 弹数     , 得 用 top() 拿

           stack_SimulateQueue_B.push(num_Pass);

      }

       Cache_Num = stack_SimulateQueue_B.top();
       stack_SimulateQueue_B.pop();

       while(!stack_SimulateQueue_B.empty())
      {
           num_Pass = stack_SimulateQueue_B.top();

           stack_SimulateQueue_B.pop();

           stack_SimulateQueue_A.push(num_Pass);

      }

       return Cache_Num ;



  }
   
   int peek() {

       int Cache_Num = -1;
                                           //int Cache_Num ;
       int num_Pass ;

       while(!stack_SimulateQueue_A.empty())
      {
           num_Pass = stack_SimulateQueue_A.top();

           stack_SimulateQueue_A.pop();

           //C++ STL Stack 的 pop 还 不管 弹数     , 得 用 top() 拿

           stack_SimulateQueue_B.push(num_Pass);

      }

       Cache_Num = stack_SimulateQueue_B.top();

       while(!stack_SimulateQueue_B.empty())
      {
           num_Pass = stack_SimulateQueue_B.top();

           stack_SimulateQueue_B.pop();

           stack_SimulateQueue_A.push(num_Pass);

      }

       return Cache_Num ;


  }
   
   bool empty() {
       return stack_SimulateQueue_A.empty();
  }
};

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/