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

智能网站推广软件兰州官网seo诊断

智能网站推广软件,兰州官网seo诊断,视频网站用什么cms,sae 企业网站文章目录 队列和栈的区别一.用队列模拟实现栈1.1入栈1.2出栈1.3返回栈顶元素1.4判断栈是否为空 二.用栈模拟实现队列2.1 入队2.2出队2.3peek2.4判断队列是否为空 三.完整代码3.1 队列模拟实现栈3.2栈模拟实现队列 队列和栈的区别 栈和队列都是常用的数据结构#xff0c;它们的… 文章目录 队列和栈的区别一.用队列模拟实现栈1.1入栈1.2出栈1.3返回栈顶元素1.4判断栈是否为空 二.用栈模拟实现队列2.1 入队2.2出队2.3peek2.4判断队列是否为空 三.完整代码3.1 队列模拟实现栈3.2栈模拟实现队列 队列和栈的区别 栈和队列都是常用的数据结构它们的主要区别在于数据的插入和删除顺序。 栈 (Stack) 是一种后进先出 (Last-In-First-Out, LIFO) 的数据结构只允许在一端进行插入和删除操作这一端称为栈顶。新元素插入后成为新的栈顶而删除时也只能删除栈顶元素。 队列 (Queue) 是一种先进先出 (First-In-First-Out, FIFO) 的数据结构允许在两端进行插入和删除操作插入在队尾删除在队头。新元素插入时成为新的队尾而删除时也只能删除队头元素。 一.用队列模拟实现栈 1.void push(int x) 将元素 x 压入栈顶。 2.int pop() 移除并返回栈顶元素。 3.int top() 返回栈顶元素。 4.boolean empty() 如果栈是空的返回 true 否则返回 false 。 如上便是需要用队列来实现栈的四个基本操作。 我们试想实现这些栈的操作一个队列可以完成吗 显然不可以我们使用两个队列来实现栈的模拟 大体流程 1.入栈时 如果两个都为空那么想 1.1入栈 当我们要放入18 25 35 48 这一串数字入栈时先放入18 25 35放入时选择的队列是不为空的队列模拟入队以及入栈时的状况如下图 public void push(int x) {if(empty()){queue1.offer(x);return;}if(!queue1.isEmpty()){queue1.offer(x);}else {queue2.offer(x);}}1.2出栈 此时如果我们要将35出栈时又该如何操作呢此时我们就需要用到第二个队列将队列一的前size1个元素也就是18 25)从队列一中出队放入队列二中。此时队列一中的元素为35队列二的元素为18 25 如下图。 当初栈完成时我们此时要将48入栈时又该放入哪个栈中呢我们考虑栈的特点先入后出我们将再入栈的元素放到不为空的队列中。 public int pop() {if(empty()){return -1;}if(!queue1.isEmpty()){int size queue1.size();for (int i 0; i size-1; i) {queue2.offer(queue1.poll());}return queue1.poll();}else {int size queue2.size();for (int i 0; i size-1; i) {queue1.offer(queue2.poll());}return queue2.poll();}} 1.3返回栈顶元素 在实现pop的基础上我们将声明一个变量temp来储存每次要移除的元素。 public int top() {if(empty()){return -1;}if (!queue1.isEmpty()){int temp -1;int size queue1.size();for (int i 0; i size; i) {temp queue1.poll();queue2.offer(temp);}return temp;}else {int size queue2.size();int temp -1;for (int i 0; i size; i) {temp queue2.poll();queue1.offer(temp);}return temp;}}1.4判断栈是否为空 当队列一和队列二都为空时此时栈就为空。 public boolean empty() {return queue1.isEmpty()queue2.isEmpty();}二.用栈模拟实现队列 我们也是用两个栈来模拟实现队列 2.1 入队 我们将所有入队的元素都放入栈一中如下图 public void push(int x) {stack1.push(x);}2.2出队 要出栈时如果栈二不为空就出栈二中的元素如果栈二为空将栈一中的所有元素一次性的全部push到栈二中此时就将入栈的元素全部倒转过来了,(例如入栈时在栈中的入栈顺序依次排序为18 25 35栈二中此时的元素入栈顺序是35 25 18出栈时就先出18,就完成了转换如下图 public int pop() {if(empty()){return -1;}if (stack2.isEmpty()){while (!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.pop();}2.3peek peek只是将出队时的pop换成peek就可以完成要求。 public int peek() {if(empty()){return -1;}if (stack2.isEmpty()){while (!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.peek();}2.4判断队列是否为空 如果栈一和栈二都为空时那么队列就为空。 public boolean empty() {return stack1.isEmpty() stack2.isEmpty();}三.完整代码 3.1 队列模拟实现栈 class MyStack {QueueInteger queue1 ;QueueInteger queue2 ;public MyStack() {queue1 new LinkedList();queue2 new LinkedList();}public void push(int x) {if(empty()){queue1.offer(x);return;}if(!queue1.isEmpty()){queue1.offer(x);}else {queue2.offer(x);}}public int pop() {if(empty()){return -1;}if(!queue1.isEmpty()){int size queue1.size();for (int i 0; i size-1; i) {queue2.offer(queue1.poll());}return queue1.poll();}else {int size queue2.size();for (int i 0; i size-1; i) {queue1.offer(queue2.poll());}return queue2.poll();}}public int top() {if(empty()){return -1;}if (!queue1.isEmpty()){int temp -1;int size queue1.size();for (int i 0; i size; i) {temp queue1.poll();queue2.offer(temp);}return temp;}else {int size queue2.size();int temp -1;for (int i 0; i size; i) {temp queue2.poll();queue1.offer(temp);}return temp;}}public boolean empty() {return queue1.isEmpty()queue2.isEmpty();} }3.2栈模拟实现队列 class MyQueue {public StackInteger stack1 ;public StackInteger stack2;public MyQueue() {stack1 new Stack();stack2 new Stack();}public void push(int x) {stack1.push(x);}public int pop() {if(empty()){return -1;}if (stack2.isEmpty()){while (!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.pop();}public int peek() {if(empty()){return -1;}if (stack2.isEmpty()){while (!stack1.isEmpty()){stack2.push(stack1.pop());}}return stack2.peek();}public boolean empty() {return stack1.isEmpty() stack2.isEmpty();} }
http://www.ho-use.cn/article/10818237.html

相关文章:

  • 住房和城乡建设部网站职称查询网站建设和管理颁奖
  • 在线免费网站模板提高工作效率整改措施
  • dw怎么做网站相册投资公司注册资金要求
  • 东莞高端品牌网站建设网站的回到顶部怎么做
  • cad如何做图纸模板下载网站系统网站设计
  • html移动网站开发网站开发专业术语大全
  • wordpress建设的网站涪陵网站制作
  • 放单网站百度发广告怎么发
  • 人社门户网站建设方案网站建设做的好的公司
  • 照明做外贸的有那些网站php 视频播放网站开发
  • 关于网站内容建设的正确说法网站开发网上教学
  • 网站设计公司请示深圳网站建设哪些
  • 南京网站微信建设网站开发排行
  • 做网站每一步的是什么安全的网站制作公司
  • 作风建设简报--门户网站wordpress api下载文件
  • 建筑案例网站有哪些网站安全检测百度
  • 用nodejs做的网站千牛网站上的店铺推广怎么做
  • 网站开发的经济效益分析2018年政务公开与网站建设总结
  • 做网站湖州电商网站网址
  • 张家港设计网站网站模板 酒类
  • 俄罗斯门户网站百度网站推广
  • php网站开发打不开Wordpress 新建模块
  • 网站编辑 图片批量销售管理软件app
  • 新网站seo技术手机做任务网站
  • 做网站建设一年能赚多少钱网站定制开发公司推荐
  • 山东省建设执业师网站北京校园文化设计公司
  • 手机网站要备案吗wordpress vip查看插件
  • 破解网站后台密码有人做吗怎么导入网站源码
  • 东菀高端网站建设做网站前期需要什么
  • 手机自助建站系统随州建设网站