当前位置:网站首页>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 .
边栏推荐
- Introduction to cesium
- Room database migration
- [paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
- Offline base tile, which can be used for cesium loading
- Practical skills -- some solutions to small problems
- JS cannot get content disposition in headers
- SAP queries open Po (open purchase order)
- Configuring WAPI certificate security policy for Huawei wireless devices
- About gbase automatically closing the connection
- 【软件测试】包装简历从这几点出发、提升通过率
猜你喜欢

【Unity入门计划】基本概念-预制件 Prefab

Native form submission data

纳米数据足球数据,足球赛事比分,体育数据api,卡塔尔世界杯
![[unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector](/img/b0/ef084f4391795a1369b0f46a2de8d6.png)
[unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
![[unity entry plan] interface Introduction (1) -scene view](/img/88/dee292cb90cd740640018e7260107f.png)
[unity entry plan] interface Introduction (1) -scene view

New version 8.6 SEO quick release system (can be built at source level)
![[unity introduction program] basic concept - preform prefab](/img/c6/aac7bffdf99073978f9b2f8ff15fbb.png)
[unity introduction program] basic concept - preform prefab

list的模拟实现
![[software testing] package resume from these points to improve the pass rate](/img/69/b27255c303150430df467ff3b5cd08.gif)
[software testing] package resume from these points to improve the pass rate

How to use network installation to deploy multiple virtual servers in KVM environment
随机推荐
Room database migration
9 best engineering construction project management systems
What if Oracle 19C migration encounters large lob tables?
【Unity入门计划】界面介绍(1)-Scene视图
设计一个有getMin功能的栈
Simulation Implementation of list
[programmer 2 Civil Servant] III. resource collection
On the peak night of the 8 Oracle ace gathering, what technology hotspots did you talk about?
[notes] search rotation sort array
[unity entry program] make my first little game
大佬秋招面经
Use of toolbar
由两个栈组成的队列
Completely replace the redis+ database architecture, and JD 618 is stable!
[unity introduction program] basic concepts GameObject & components
ACNet:用于图像超分的非对称卷积(附实现code)
Design of workflow system
[programmer 2 Civil Servant] summary of some common problems about system research
Learn no when playing 10. Is enterprise knowledge management too boring? Use it to solve!
New functions of shixizhi are online. These new functions are online in June. Can you use them?