当前位置:网站首页>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(); */

边栏推荐
- Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
- .net operation redis string string
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
- mysql20210906
- Sql Server 数据库之完整性约束
- Zongzi battle - guess who can win
- Sql Server之查询总结
- 【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
- Build ARM embedded development environment
- 剑指Offer(四十三):左旋转字符串
猜你喜欢

英语基础句型结构------起源

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题

sigmod 函数与softmax 函数对比

RT thread learning notes (VI) -- start the elmfat file system based on SPI flash (Part 1)

RT-Thread 学习笔记(三)---用SCons 构建编译环境

第6期:大学生应该选择哪种主流编程语言

RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)

粽子大战 —— 猜猜谁能赢

kali 查看ip地址

Disable usbjatg in Altium Designer
随机推荐
RT thread learning notes (I) -- configure RT thread development environment
winpcap 抓包函数pcap_loop(),停止问题
剑指Offer(四十九):把字符串转换成整数
粽子大战 —— 猜猜谁能赢
[leetcode daily question 2021/4/23]368. Maximum divisible subset
2021-08-14三子棋
使用定位实现左中右布局,中间内容自适应
Anaconda is used on vscode (the environment has been configured)
RT thread learning notes (III) -- building a compilation environment with scons
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
[dectectron2] follow the official demo
Uninstall Meizu app store
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
对面向抽象编程的理解
使用float实现左中右布局,中间内容自适应
第4期:大学生提前职业技能准备之一
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
MD5 encryption
C language callback function
MySQL速学-2021-09-01