当前位置:网站首页>A queue of two stacks
A queue of two stacks
2022-07-25 07:39:00 【dlz456】
subject : Write a class , Queues are implemented with two stacks , Support the basic operation of queue
answer : Stack is first in, first out , The queue is in and out . With two stacks , You must require the reverse order of all the numbers in one stack from another stack . Such a stack is only responsible for pressing , The other stack is only responsible for ejecting .
But be careful , You have to wait until all the elements are completely on a stack , Can operate on another stack , Make the order of elements in it opposite to the first stack . And if the second stack is not empty , It can't be operated on .
See code for specific implementation :
package text2;
import java.util.Stack;
public class TwoStackQueue {
public Stack<Integer> stackpush;
public Stack<Integer> stackpop;
public TwoStackQueue() {
stackpush=new Stack<Integer>();
stackpop=new Stack<Integer>();
}
private void pushToPop() {
if (stackpop.empty()) {
while(!stackpush.empty()) {
stackpop.push(stackpush.pop());
}
}
}
public void add(int pushint) {
stackpush.push(pushint);
pushToPop();
}
public int poll() {
if(stackpop.empty()&&stackpush.empty()) {
throw new RuntimeException("Queue is empty.");
}
pushToPop();
return stackpop.pop();
}
public int peek() {
if(stackpop.empty()&&stackpush.empty()) {
throw new RuntimeException("Queue is empty.");
}
pushToPop();
return stackpop.peek();
}
}
In the above code , Stack used empty() and isempty() There is no difference between the two functions . That is, when the stack is empty , Use both functions .
But there is only isempty Method .
边栏推荐
- 【Unity入门计划】制作我的第一个小游戏
- 全新8.6版本SEO快排系统(可源码级搭建)
- NLP hotspots from ACL 2022 onsite experience
- 交叉熵计算公式
- JS note 17: the whole process of jest project configuration of typescript project
- Is the yield of financial products high or low?
- Tips - prevent system problems and file loss
- 【Unity入门计划】基本概念-GameObject&Components
- 【程序员2公务员】四、常见问题
- 【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
猜你喜欢
![[paper notes] effective CNN architecture design guided by visualization](/img/aa/aeeac3f970eac7f110987c523602c8.png)
[paper notes] effective CNN architecture design guided by visualization

12 combination methods and risk interpretation of database architecture optimization (books available)

What if Oracle 19C migration encounters large lob tables?

cesium简介

【微信小程序】全局样式、局部样式、全局配置

Bingbing's learning notes: classes and objects (Part 1)
![[paper notes] next vit: next generation vision transformer for efficient deployment in real industry](/img/ea/56881999a90f9c65f5f8768f9574bd.png)
[paper notes] next vit: next generation vision transformer for efficient deployment in real industry

Robot framework mobile terminal Automation Test ----- 01 environment installation
![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
[unity entry program] basic concept trigger

A fast method of data set enhancement for deep learning
随机推荐
[unity introduction program] basic concept - preform prefab
【程序员2公务员】四、常见问题
MathWorks has been in China for 15 years. What are the secrets of continuous innovation?
Practical operation: elegant downtime under large-scale micro service architecture
【Unity入门计划】制作我的第一个小游戏
[paper notes] next vit: next generation vision transformer for efficient deployment in real industry
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
整数a按位取反(~)后的值为-(a+1)
线代(矩阵‘)
J1 common DOS commands (P25)
Offline base tile, which can be used for cesium loading
ACNet:用于图像超分的非对称卷积(附实现code)
Tips - prevent system problems and file loss
QT learning diary 20 - aircraft war project
【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
Nano data, football data, football match scores, sports data API, Qatar world cup
【Unity入门计划】基本概念-触发器 Trigger
UXDB怎么从日期值中提取时分秒?
P1049 [NOIP2001 普及组 T4] 装箱问题
On the peak night of the 8 Oracle ace gathering, what technology hotspots did you talk about?