【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 };