【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks

 1 class Queue {
 2 public:
 3     // Push element x to the back of queue.
 4     void push(int x) {
 5         while (!nums.empty()) {
 6             nums_assist.push(nums.top());
 7             nums.pop();
 8         }
 9         nums.push(x);
10         while (!nums_assist.empty()) {
11             nums.push(nums_assist.top());
12             nums_assist.pop();
13         }
14     }
15 
16     // Removes the element from in front of queue.
17     void pop(void) {
18         nums.pop();
19     }
20 
21     // Get the front element.
22     int peek(void) {
23         return nums.top();
24     }
25 
26     // Return whether the queue is empty.
27     bool empty(void) {
28         return nums.empty();
29     }
30 private:
31     stack<int> nums, nums_assist;
32 };

 

相关文章:

  • 2022-01-25
  • 2022-01-30
  • 2021-07-06
  • 2022-12-23
  • 2021-07-24
  • 2021-12-13
  • 2021-10-08
  • 2022-01-16
猜你喜欢
  • 2021-09-25
  • 2021-07-18
  • 2021-10-23
  • 2021-12-23
  • 2022-02-02
  • 2021-07-29
  • 2021-09-29
相关资源
相似解决方案