当前位置:网站首页>232.用栈实现队列
232.用栈实现队列
2022-07-26 10:42:00 【Forest_1010】

class MyQueue {
public:
stack<int> stack1;
stack<int> stack2;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(stack2.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
}
int result = stack2.top();
stack2.pop();
while(!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
return result;
}
/** Get the front element. */
int peek() {
if(stack2.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
}
int result=stack2.top();
while(!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
return result;
}
/** Returns whether the queue is empty. */
bool empty() {
if(stack1.empty()&&stack2.empty())
return true;
else
return false;
}
};
/** * Your MyQueue object will be instantiated and called as such: * MyQueue* obj = new MyQueue(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->peek(); * bool param_4 = obj->empty(); */

边栏推荐
- 使用flex实现左中右布局,中间自适应
- kali 查看ip地址
- [dectectron2] follow the official demo
- Error[Pe147]: declaration is incompatible with '错误问题
- 一文详解Nodejs中fs文件模块与path路径模块
- Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
- 2021-08-12函数递归_和鹏哥学习C语言
- .net operation redis set unordered collection
- 剑指Offer(八):跳台阶
- Analysis of the transaction problem of chained method call
猜你喜欢

RT-Thread 学习笔记(一)---配置RT-Thread开发环境
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
![[leetcode daily question 2021/4/29]403. Frogs cross the river](/img/fb/612777c77df5a611506e72f4f4c3c8.png)
[leetcode daily question 2021/4/29]403. Frogs cross the river
![[dectectron2] follow the official demo](/img/aa/03e46897234c309415b336ac39b7d9.png)
[dectectron2] follow the official demo
![Error[pe147]: declaration is incompatible with 'error problem](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[pe147]: declaration is incompatible with 'error problem
![[paper after dinner] deep mining external perfect data for chestx ray disease screening](/img/d6/41c75d292c26b2e7e116767a51eb5e.png)
[paper after dinner] deep mining external perfect data for chestx ray disease screening

Anaconda is used on vscode (the environment has been configured)

.net5wtm (asp.net core) PgSQL unpacking operation

在神州IV开发板上为STemWin 5.22加入触屏驱动

英语基础句型结构------起源
随机推荐
超图 影像 如何去除黑边(两种方法)
在神州IV开发板上为STemWin 5.22加入触屏驱动
控制随机抽中几率 [ C# | Random ]
Sql Server 数据库之数据类型
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
第7期:内卷和躺平,你怎么选
display-inline+calc实现左中右布局,中间自适应
第4期:大学生提前职业技能准备之一
7-25 0-1背包 (50分)
Halcon模板匹配之Shape
[dectectron2] follow the official demo
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
Koin
$router和$route的区别
Issue 8: cloud native -- how should college students learn in the workplace
11 handle "self assignment" in operator=
(转载)ArcGIS Engine中各种点的创建方法
Oracle create index
MFC图片控件