当前位置:网站首页>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 .
边栏推荐
- Room database migration
- How should enterprise users choose aiops or APM?
- A fast method of data set enhancement for deep learning
- [programmer 2 Civil Servant] III. resource collection
- Use of toolbar
- On the peak night of the 8 Oracle ace gathering, what technology hotspots did you talk about?
- Completely replace the redis+ database architecture, and JD 618 is stable!
- [unity entry plan] interface Introduction (1) -scene view
- Common cross domain scenarios
- [unity introduction program] basic concepts GameObject & components
猜你喜欢

Analysis of difficulties in diagramscene project

【Unity入门计划】界面介绍(1)-Scene视图

What if Oracle 19C migration encounters large lob tables?

Introduction to Manhattan distance

Nailing the latest version, how to clear the login phone number history data

Gather the wisdom of developers and consolidate the foundation of the database industry

华为无线设备配置WAPI-证书安全策略
![[unity introduction program] basic concepts -2d rigid body 2D](/img/67/537e9935bb0f2539945032cadcf232.png)
[unity introduction program] basic concepts -2d rigid body 2D

【论文笔记】EFFICIENT CNN ARCHITECTURE DESIGN GUIDED BY VISUALIZATION

J1 common DOS commands (P25)
随机推荐
Gan series of confrontation generation network -- Gan principle and small case of handwritten digit generation
Teach you to use cann to convert photos into cartoon style
NLP hotspots from ACL 2022 onsite experience
线代(矩阵‘)
Lidar construction map (overlay grid construction map)
[unity introduction program] basic concepts GameObject & components
Huawei wireless device configuration wpa2-802.1x-aes security policy
First, how about qifujin
J1 常用的DOS命令(P25)
list的模拟实现
[notes for question brushing] search the insertion position (flexible use of dichotomy)
Cluster chat server: summary of project problems
[wechat applet] global style, local style, global configuration
What if Oracle 19C migration encounters large lob tables?
About --skip networking in gbase 8A
Matlab self programming series (1) -- angular distribution function
【程序员2公务员】四、常见问题
Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
Native form submission data
大佬秋招面经