当前位置:网站首页>由两个栈组成的队列
由两个栈组成的队列
2022-07-25 07:38:00 【dlz456】
题目:编写一个类,用两个栈实现队列,支持队列的基本操作
解答:栈是先进先出,队列是先进后出。用两个栈来实现的话,必须要求一个栈里面的所有数的顺序的另一个栈的倒序。这样一个栈只负责压入,另一个栈只负责弹出。
但是要注意,必须等到所有的元素完全进入一个栈的时候,才可以对另一个栈进行操作,使其里面的元素顺序和第一个栈相反。并且如果第二个栈不为空的话,是不能对其进行操作的。
具体实现看代码:
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();
}
}
上述代码中,栈使用的empty()和isempty()两个函数是没有区别的。也就是再判断栈空的时候,使用两个函数均可。
但是字符串中只有isempty方法。
边栏推荐
- 【ES6】函数的参数、Symbol数据类型、迭代器与生成器
- 【程序员2公务员】一、基本认知
- What are runtimecompiler and runtimeonly
- [programmer 2 Civil Servant] IV. common problems
- Huawei wireless device sta black and white list configuration command
- Load capacity - sorting out the mind map that affects load capacity
- Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
- When deep learning makes data sets, it specifies how many frames to extract an image from the long video to the specified file path
- Summary of differences between data submission type request payload and form data
- What are the types of financial products in 2022? Which is suitable for beginners?
猜你喜欢

深度学习之快速实现数据集增强的方法

nanodet训练时出现问题:ModuleNotFoundError: No module named ‘nanodet‘的解决方法

How should enterprise users choose aiops or APM?

Robot framework mobile terminal Automation Test ----- 01 environment installation

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

Robot Framework移动端自动化测试----01环境安装

First, how about qifujin

Teach you to use cann to convert photos into cartoon style

QT learning diary 20 - aircraft war project

QT学习日记20——飞机大战项目
随机推荐
What if Oracle 19C migration encounters large lob tables?
Gather the wisdom of developers and consolidate the foundation of the database industry
Line generation (matrix ')
【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
Huawei wireless device configuration wpa2-802.1x-aes security policy
Robot Framework移动端自动化测试----01环境安装
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
转行学什么成为了一大部分人的难题,那么为什么很多人学习软件测试呢?
3. Promise
大佬秋招面经
Design of workflow system
How to do a good job in safety development?
深度学习训练和测试时出现问题:error: the following arguments are required: --dataroot,解决:训练文件的配置方法和测试文件的配置方法
2-6. Automatic acquisition
[notes] search rotation sort array
Leetcode118. Yanghui triangle
A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking
Learn no when playing 10. Is enterprise knowledge management too boring? Use it to solve!
【Unity入门计划】基本概念-预制件 Prefab