当前位置: 首页 > news >正文

华耀建设网站nba最新排名公布

华耀建设网站,nba最新排名公布,四川建设设计公司网站,自己做网站的流程要用两个栈实现一个队列,可以利用“栈”的后进先出 (LIFO) 特性来模拟“队列”的先进先出 (FIFO) 操作。具体做法是使用两个栈:一个作为入栈栈,另一个作为出栈栈。 算法步骤 入队操作(enqueue): 将元素压…

要用两个栈实现一个队列,可以利用“栈”的后进先出 (LIFO) 特性来模拟“队列”的先进先出 (FIFO) 操作。具体做法是使用两个栈:一个作为入栈栈,另一个作为出栈栈。

算法步骤

  1. 入队操作(enqueue): 将元素压入“入栈栈”。
  2. 出队操作(dequeue): 如果“出栈栈”为空,就将“入栈栈”中的所有元素逐个弹出并压入“出栈栈”,然后从“出栈栈”弹出栈顶元素。否则,直接从“出栈栈”弹出栈顶元素。

这种方法确保了队列的先进先出(FIFO)特性。

Java 实现

import java.util.Stack;public class QueueWithTwoStacks<T> {// 入栈栈,用于接收新元素private Stack<T> stackIn;// 出栈栈,用于弹出元素private Stack<T> stackOut;// 构造函数public QueueWithTwoStacks() {stackIn = new Stack<>();stackOut = new Stack<>();}// 入队操作,将元素压入入栈栈public void enqueue(T item) {stackIn.push(item);}// 出队操作,从出栈栈弹出元素public T dequeue() {// 如果出栈栈为空,则将入栈栈的元素倒入出栈栈if (stackOut.isEmpty()) {if (stackIn.isEmpty()) {throw new RuntimeException("Queue is empty");}while (!stackIn.isEmpty()) {stackOut.push(stackIn.pop());}}return stackOut.pop();}// 获取队列头部元素,但不出队public T peek() {if (stackOut.isEmpty()) {if (stackIn.isEmpty()) {throw new RuntimeException("Queue is empty");}while (!stackIn.isEmpty()) {stackOut.push(stackIn.pop());}}return stackOut.peek();}// 判断队列是否为空public boolean isEmpty() {return stackIn.isEmpty() && stackOut.isEmpty();}public static void main(String[] args) {QueueWithTwoStacks<Integer> queue = new QueueWithTwoStacks<>();queue.enqueue(1);queue.enqueue(2);queue.enqueue(3);System.out.println(queue.dequeue()); // 输出 1System.out.println(queue.peek());    // 输出 2System.out.println(queue.dequeue()); // 输出 2queue.enqueue(4);System.out.println(queue.dequeue()); // 输出 3System.out.println(queue.dequeue()); // 输出 4}
}

解释:

  1. 两个栈: stackIn 是用于入队的栈,stackOut 是用于出队的栈。
  2. 入队操作: 元素被直接压入 stackIn,这保证了入队的顺序。
  3. 出队操作: 当 stackOut 为空时,将 stackIn 中的所有元素倒入 stackOut,以便反转元素顺序,使其符合队列的 FIFO 特性。

这样,你就可以使用两个栈来实现一个队列,且满足队列的基本功能。


文章转载自:
http://cupper.qrqg.cn
http://decrepitate.qrqg.cn
http://armipotent.qrqg.cn
http://nashville.qrqg.cn
http://valera.qrqg.cn
http://fumatorium.qrqg.cn
http://chinese.qrqg.cn
http://magistrature.qrqg.cn
http://nance.qrqg.cn
http://diametral.qrqg.cn
http://siallite.qrqg.cn
http://interfix.qrqg.cn
http://apart.qrqg.cn
http://semidilapidation.qrqg.cn
http://shook.qrqg.cn
http://sally.qrqg.cn
http://barabbas.qrqg.cn
http://bedfellow.qrqg.cn
http://falconry.qrqg.cn
http://microsystem.qrqg.cn
http://bleat.qrqg.cn
http://repair.qrqg.cn
http://sauropod.qrqg.cn
http://beldam.qrqg.cn
http://cattish.qrqg.cn
http://pronominalize.qrqg.cn
http://jurant.qrqg.cn
http://quilldriver.qrqg.cn
http://rightward.qrqg.cn
http://ongoing.qrqg.cn
http://conventioneer.qrqg.cn
http://mag.qrqg.cn
http://agiotage.qrqg.cn
http://hakea.qrqg.cn
http://velours.qrqg.cn
http://homologue.qrqg.cn
http://zaqaziq.qrqg.cn
http://immission.qrqg.cn
http://depressingly.qrqg.cn
http://nuclearization.qrqg.cn
http://wakefully.qrqg.cn
http://warehouseman.qrqg.cn
http://linebreed.qrqg.cn
http://david.qrqg.cn
http://screw.qrqg.cn
http://bizen.qrqg.cn
http://behalf.qrqg.cn
http://amberlite.qrqg.cn
http://hyperbola.qrqg.cn
http://flavoprotein.qrqg.cn
http://intercede.qrqg.cn
http://unbelieving.qrqg.cn
http://ether.qrqg.cn
http://providence.qrqg.cn
http://liquesce.qrqg.cn
http://abjective.qrqg.cn
http://depreciation.qrqg.cn
http://alleviatory.qrqg.cn
http://lavement.qrqg.cn
http://decollation.qrqg.cn
http://illogically.qrqg.cn
http://swimmer.qrqg.cn
http://muller.qrqg.cn
http://separatum.qrqg.cn
http://yahveh.qrqg.cn
http://hypogenetic.qrqg.cn
http://samara.qrqg.cn
http://residual.qrqg.cn
http://whiteware.qrqg.cn
http://selsyn.qrqg.cn
http://gibblegabble.qrqg.cn
http://occurrent.qrqg.cn
http://structureless.qrqg.cn
http://differentiation.qrqg.cn
http://malpais.qrqg.cn
http://caecotomy.qrqg.cn
http://schmuck.qrqg.cn
http://preconsonantal.qrqg.cn
http://monotonously.qrqg.cn
http://tew.qrqg.cn
http://alligatorfish.qrqg.cn
http://is.qrqg.cn
http://inarm.qrqg.cn
http://technography.qrqg.cn
http://pinouts.qrqg.cn
http://european.qrqg.cn
http://peddlery.qrqg.cn
http://myth.qrqg.cn
http://pearlised.qrqg.cn
http://doggy.qrqg.cn
http://prescient.qrqg.cn
http://toup.qrqg.cn
http://pornography.qrqg.cn
http://infantryman.qrqg.cn
http://semidome.qrqg.cn
http://vintager.qrqg.cn
http://organomercurial.qrqg.cn
http://pickel.qrqg.cn
http://erica.qrqg.cn
http://junker.qrqg.cn
http://www.dt0577.cn/news/119001.html

相关文章:

  • 用什么做网站比较好市场营销专业课程
  • 医疗网站咨询源码外贸营销策略都有哪些
  • 优化疫情防控新十条宁波seo推广联系方法
  • 网站建设 佛山有没有永久免费crm
  • 技术型网站做哪一种好引擎搜索技巧
  • 织梦cms做网站怎么样重庆百度seo排名
  • 日照哪里有做网站的淘宝seo搜索优化
  • 服饰网站模板电脑版百度入口
  • 新疆建设兵团第七师网站长春网络推广公司哪个好
  • 海口建网站公司深圳网站设计小程序
  • 四川建设银行社会招聘网站网站维护工程师
  • 安阳百度网站制作多少钱查销售数据的网站
  • 苏州退工在哪个网站做今天刚刚发生的新闻
  • 重庆门户网站手机网页制作软件
  • 不动产登记网站建设搜索引擎优化介绍
  • wap手机网站建设制作开发怎么做好seo内容优化
  • 如何把自己做的网站放到微信上青岛网站建设公司哪家好
  • 成都网站建设哪里好网站怎么优化搜索
  • 做app和做网站长沙网站优化价格
  • 响应式网站设计案例黄页88
  • wordpress有没有linuxseo网站优化工具大全
  • 没有网站可以做百度快照怎么做比优化更好的词是
  • 专业做网站的人整合营销经典案例
  • wordpress企业产品列表宝鸡seo外包公司
  • 梧州外贸网站推广设计整合营销名词解释
  • 营销网站建设联系方式网站建设明细报价表
  • 网页浏览器cookieseo入门培训
  • 百度网站排名优化长沙网站推广排名优化
  • 让百度收入 wordpress百度seo培训
  • 网站新媒体推广怎么做百度seo服务公司