Implementing Queue using Stacks

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

    • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
    • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
    • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

Analyse: Using two stacks: in & out to store the information. IN stack is used to push element while OUT stack is used to pop out element. If two stacks are both empty, then the queue is empty.

 1 class Queue {
 2 private:
 3     stack<int> in;
 4     stack<int> out;
 5     void move(){//move all elements in the IN stack to the OUT stack
 6         while(!in.empty()){
 7             out.push(in.top());
 8             in.pop();
 9         }
10     }
11 public:
12     // Push element x to the back of queue.
13     void push(int x) {
14         in.push(x);
15     }
16
17     // Removes the element from in front of queue.
18     void pop(void) {//be careful about the condition
19         if(out.empty()) move(); //if the out stack is empty, then move all elements from IN stack to OUT stack
20         if(!out.empty()) out.pop();//if the out stack is not empty, then pop out the top element
21     }
22
23     // Get the front element.
24     int peek(void) {
25         if(out.empty()) move();
26         if(!out.empty()) return out.top();
27     }
28
29     // Return whether the queue is empty.
30     bool empty(void) {
31         return in.empty() && out.empty();
32     }
33 };
时间: 2024-09-30 17:29:08

Implementing Queue using Stacks的相关文章

232. Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

232. 用栈实现队列 Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

Leetcode题目:Implement Queue using Stacks

题目:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

leetcode Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

[LeetCode] 232 - Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

LeetCode232:Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. empty() – Return whether the queue is empty. Notes: You mus

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

[LeetCode] 232. Implement Queue using Stacks Java

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

【一天一道LeetCode】#232. Implement Queue using Stacks

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue