当前位置:网站首页>Force buckle: implement a queue with two stacks
Force buckle: implement a queue with two stacks
2022-06-22 03:41:00 【Yu who wants to fly】
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue(){
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
if(stack2.isEmpty()){
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
if(stack2.isEmpty()){
return -1;
}else {
return stack2.pop();
}
}
}
This problem is very clear, using two stacks to achieve a queue , Almost the implementation method has been given .
The feature of the stack is first in, then out , The characteristics of queues are first in, first out .
Very clear , It only needs to be put into the stack twice , First in, then out , First in, then out , The natural result is first in, first out , Same as the result of the queue .
Others are questions of logical judgment .
边栏推荐
- std::move与std::forward右值引用研究
- Summary of image classification based on pytoch: swing transformer
- 1690. stone game vii- dynamic programming method
- Zombie process and orphan process
- ES next 新特性
- Explanation of atomic operation in golang concurrent programming
- A component required a bean of type 'com.example.demo3.service.UserServiceImp' that could not be fou
- c# 自定义排序
- When 618 attacks, how to choose between Beibei X3 and Jimi h3s? Take you all-round in-depth analysis
- 魔法方法《六》__enter__和__exit__
猜你喜欢
随机推荐
C language integer value range - the problem of more than one negative number
3DE new simulation status
std::make_ Shared features
How did we solve the realsense color bias problem?
二叉树的层次遍历
3de 保存到收藏夹
基于logback.xml实现保存日志信息的无感操作
Shelling of ESP law of reverse crackme
在请求目标中找到无效字符。有效字符在RFC 7230和RFC 3986中定义
Implementation of synchronization and atomic operation by mutex mutex in golang concurrent programming
倍福TwinCAT3中PLC程序变量定义和硬件IO关联
存算一体芯片离普及还有多远?听听从业者怎么说 | 对撞派 x 后摩智能
Splunk: Auto load Balanced TCP Output issue
内网穿透
1690. stone game vii- dynamic programming method
svn高效管理怎么实现
Zombie process and orphan process
分析Iceberg合并任务解决数据冲突
rabbmitMQ 发布关键字模式<三>
装饰器《二》 property - 简答逻辑









