leetcode_225题——Implement Stack using Queues (队列,栈)

Implement Stack using Queues

Total Accepted: 9609 Total Submissions: 31710My Submissions

Question Solution

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

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

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

Hide Tags

Stack Data Structure

Hide Similar Problems

(E) Implement Queue using Stacks

这道题要用队列来实现栈,所以只需要用一个队列就可以了

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4
 5 queue<int> qu1;
 6
 7  // Push element x onto stack.
 8     void push(int x) {
 9         qu1.push(x);
10     }
11
12     // Removes the element on top of the stack.
13     void pop() {
14         if(!qu1.empty())
15         {
16             int n=qu1.size()-1;
17             int re;
18             while(n--)
19             {
20                 re=qu1.front();
21                 qu1.pop();
22                 qu1.push(re);
23             }
24             qu1.pop();
25         }
26         else
27             return;
28     }
29
30     // Get the top element.
31     int top() {
32         int n=qu1.size();
33         int re;
34         int res;
35         while(n--)
36         {
37             re=qu1.front();
38             if(n==0)
39                 res=re;
40             qu1.pop();
41             qu1.push(re);
42         }
43         return res;
44     }
45
46     // Return whether the stack is empty.
47     bool empty() {
48         if(qu1.empty())
49             return 1;
50         else
51             return 0;
52     }
53
54
55 int main()
56 {
57
58 }
时间: 2024-08-07 06:39:03

leetcode_225题——Implement Stack using Queues (队列,栈)的相关文章

[LeetCode][JavaScript]Implement Stack using Queues

Implement Stack using Queues Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empt

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

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 whet

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

Implement Stack using Queues 用队列实现栈

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. 解题思路: 用队列实现栈的操作.包括输入.输出.取栈

leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

leetcode Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

LeetCode225:Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether the stack is empty. Notes: You must use only stand

[leetcode] 225. Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use only s

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empt