LeetCode Implement Queue using Stacks (数据结构)

题意:

  用栈来实现队列。

思路:

  一个栈是不够的,至少要两个。

  (1)插入。永远只插入到stack1中(插到栈顶)。

  (2)弹出。如果stack2不为空,直接弹出stack2的栈顶,否则,将stack1中的所有元素转移到stack2中,栈顶自然就是队头了,再弹出。

  (3)返回队头。与(2)一样。

  (4)是否为空。判断两个栈是否同时为空即可。

  只要保证stack2为空时才可以将stack1转移到stack2中,就可以保证两边并不会产生混乱而出错。

 1 class Queue {
 2 /*
 3     // Push element x to the back of queue.
 4     void push(int x) {
 5
 6     }
 7
 8     // Removes the element from in front of queue.
 9     void pop(void) {
10
11     }
12
13     // Get the front element.
14     int peek(void) {
15
16     }
17
18     // Return whether the queue is empty.
19     bool empty(void) {
20
21     }
22 */
23     stack<int> stack1,stack2;
24 public:
25     bool change()//当stack2为空时,将stack1转到stack2中
26     {
27         while(!stack1.empty())
28         {
29             stack2.push(stack1.top());
30             stack1.pop();
31         }
32         if(stack2.empty())    return true;
33         else    return false;
34     }
35     void push(int x)
36     {
37         stack1.push(x);
38     }
39     void pop(void)
40     {
41         if(!stack2.empty())    stack2.pop();
42         else if(!change())    stack2.pop();
43     }
44     int peek(void)
45     {
46         if(!stack2.empty())    return stack2.top();
47         else
48         {
49             if(!change())    return stack2.top();
50             return 0;
51         }
52     }
53     bool empty(void)
54     {
55         if(stack1.empty()&&stack2.empty())    return true;
56         else    return false;
57     }
58 };

AC代码

时间: 2024-10-19 15:36:05

LeetCode Implement Queue using Stacks (数据结构)的相关文章

[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 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][JavaScript]Implement Queue using Stacks

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 t

【LeetCode】232. Implement Queue using Stacks

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 t

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

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

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

【一天一道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