第五章 栈与队列part01

第五章 栈与队列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();
*/

 

 

 

225. 用队列实现栈 (单队列模拟)

 

Need To Attention :

// 这里 C++ STL queue 队列 首部 元素 的 获取 使用 的 是 front() 而不是 top()

// push 和 pop 的 函数名/ 操作 名 被 沿用 , 但 功能 与 具体 实现 不同

 

Code :

class MyStack {
public:

   queue<int> queue_SimulateStack;


   MyStack() {

  }
   
   void push(int x) {
       queue_SimulateStack.push(x);
  }
   
   int pop() {
       int len_queue_SimulateStack = queue_SimulateStack.size();

       int Cache_Target_Num ;

       int i = 0;

       int num_Pass;

       for( i = 0 ; i < ( len_queue_SimulateStack - 1 ) ; i ++ )
      {
           // 这里 C++ STL queue 队列 首部 元素 的 获取 使用 的 是 front() 而不是 top()
           // push 和 pop 的 函数名/ 操作 名 被 沿用 , 但 功能 与 具体 实现 不同
           num_Pass = queue_SimulateStack.front();
           queue_SimulateStack.pop();
           queue_SimulateStack.push(num_Pass);


      }

       Cache_Target_Num = queue_SimulateStack.front();
       queue_SimulateStack.pop();


       //   后边 的 元素 序列 其实 已经 排 好 了


       return Cache_Target_Num ;


       

  }
   
   int top() {
       int len_queue_SimulateStack = queue_SimulateStack.size();

       int Cache_Target_Num ;

       int i = 0;

       int num_Pass;

       for( i = 0 ; i < ( len_queue_SimulateStack - 1 ) ; i ++ )
      {
           num_Pass = queue_SimulateStack.front();
           queue_SimulateStack.pop();
           queue_SimulateStack.push(num_Pass);


      }

       Cache_Target_Num = queue_SimulateStack.front();
       queue_SimulateStack.pop();
       queue_SimulateStack.push(Cache_Target_Num);

       return Cache_Target_Num;

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

/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
 
posted @ 2023-12-09 19:00  晴夜空  阅读(14)  评论(0)    收藏  举报