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

在合肥做网站前端月薪大概多少钱外贸网站推广外包

在合肥做网站前端月薪大概多少钱,外贸网站推广外包,网站设计速成,维影企业网站管理系统目录 Day 18:循环队列 一、关于循环队列 1. 面对顺序表结构的妥协 2. header 与 tail 指针的巧妙设计 二、循环队列的方法 1. 结构定义及遍历 2. 入队 3. 出队 三、代码及测试 拓展: 小结 Day 18:循环队列 Task: 整除…

目录

Day 18:循环队列

一、关于循环队列

1. 面对顺序表结构的妥协

2. header 与 tail 指针的巧妙设计

二、循环队列的方法

1. 结构定义及遍历

2. 入队

3. 出队

三、代码及测试

拓展:

小结 


Day 18:循环队列

Task:

  • 整除的作用.
  • 想像操场跑道里放一队人, 循环的感觉就出来了.
  • 为了区分空队列与满队列, 需要留一个空间. 相当于不允许首尾相连. 还是画个图吧, 否则容易进坑.
  • 用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统, 从这个代码可以感受下内存的管理.

一、关于循环队列

1. 面对顺序表结构的妥协

        在 Day 17 中讲过,我们之所以使用链表来实现队列,是因为使用顺序表时左侧出队的代价太大。但我们巧妙的改进队列的结构后,一切就变得简单起来。

        具体的操作是:选择 逻辑上的删除 ,即利用双指针控制,令我们的首选元素的下标指向+1,从而在逻辑上屏蔽掉之前的位置的元素。
        但是这个逻辑删除与添加方法有个弊端就是:假设我们不断对队列进行入队、出队、入队、出队…那么我们首尾指针位置最终会变得非常大,但是队列内的数据却还是非常少,而且之前出队后空余的位置无法被重复使用,照成极大浪费。

        于是考虑用一种方法能够重用之前空余出来的无法被重复使用的空间。于是乎,我们使用了一种灵活的策略:循环队列:

        这个方案是在逻辑上将我们的线性表的首位合并构造一个环,具体上来看,只要通过 i = (i + 1) % N 就可以非常方便实现这个功能设想。

        一方面,这样我们不断增大的指针就可以重复利用之前释放的空间,避免浪费。另一方面,这个方案允许了尾指针位置在数组上大于头指针的情况,保证了N个空间能完全使用。

2. header 与 tail 指针的巧妙设计

        我们定义头指针指向head,尾指针指向tail,其中数据有效范围为[h , t),如图:

        因此,我们可以定义队空为:head == tail

        那么队满如何表示呢?好像将数据填满后,队满的表示也是: head == tail,这显然会发生冲突,所以我们做出调整,用这个语句来表示队满,即图c:(tail + 1) % N == head

二、循环队列的方法

1. 结构定义及遍历

	/*** The total space. One space can never can never be used*/public static final int TOTAL_SPACE = 10;/** The data;*/int[] data;/*** The index for calculating the head. The actual head is head % TOTAL_SPACE.*/int head;/*** The index for caluculating the tail.*/int tail;/********************* * The constructor******************* */public CircleIntQueue() {data = new int[TOTAL_SPACE];head = 0;tail = 0;}// Of the first constructor/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (head == tail) {return "empty";} // Of iffor (int i = head; i < tail; i++) {resultString += data[i % TOTAL_SPACE] + ", ";} // Of for ireturn resultString;}// Of toString

2. 入队

	/************************ Enqueue.* * @param paraValue The value of the new node.**********************/public void enqueue(int paraValue) {if((tail + 1)% TOTAL_SPACE ==  head ) {System.out.println("Queue full.");return;}// Of ifdata[tail % TOTAL_SPACE] = paraValue;tail++;}//Of enqueue

        对于顺序表结构,添加元素前判断是否满是必要操作。 

        至于添加元素所使用到的语句。其实就是基于我们之前定义的循环顺序表递增操作 i = (i + 1) % N ;`而确定的,只不过因为我们尾指针默认在无数据的位置,故是先赋值在递增。
        而且,我们的代码操作中,将取余操作从递增后立马取余推迟到了下次赋值时

3. 出队

	/************************ Dequeue.* * @return The value at the head.**********************/public int dequeue() {if(head == tail) {System.out.println("No element in the queue");return -1;}//Of ifint resultValue = data[head % TOTAL_SPACE];head++;return resultValue;}//Of dequeue

        顺序表的删除只需要关注是否为空即可。 

三、代码及测试

package datastructure.queue;/*** Circle int queue.** @author: Changyang Hu joe03@foxmail.com* @date created: 2025-05-16*/
public class CircleIntQueue {/*** The total space. One space can never be used.*/public static final int TOTAL_SPACE = 10;/*** The data.*/int[] data;/*** The index for calculating the head. The actual head is head % TOTAL_SPACE.*/int head;/*** The index for calculating the tail.*/int tail;/********************* * The constructor******************* */public CircleIntQueue() {data = new int[TOTAL_SPACE];head = 0;tail = 0;}// Of the first constructor/*** ********************** @Title: enqueue* @Description: Enqueue.** @param paraValue The value of the new node.* @return void **********************/public void enqueue(int paraValue) {if ((tail + 1) % TOTAL_SPACE == head) {System.out.println("Queue full.");return;} // Of ifdata[tail % TOTAL_SPACE] = paraValue;tail++;}// Of enqueue/*** ********************** @Title: dequeue* @Description: Dequeue.** @return The value at the head.* @return int **********************/public int dequeue() {if (head == tail) {System.out.println("No element in the queue");return -1;} // Of ifint resultValue = data[head % TOTAL_SPACE];head++;return resultValue;}// Of dequeue/************************ Overrides the method claimed in Object, the superclass of any class.**********************/public String toString() {String resultString = "";if (head == tail) {return "empty";} // Of iffor (int i = head; i < tail; i++) {resultString += data[i % TOTAL_SPACE] + ", ";} // Of for ireturn resultString;}// Of toString/*** ********************** @Title: main* @Description: The entrance of the program.** @param args Not used now.* @return void **********************/public static void main(String args[]) {CircleIntQueue tempQueue = new CircleIntQueue();System.out.println("Initialized, the list is: " + tempQueue.toString());for (int i = 0; i < 5; i++) {tempQueue.enqueue(i + 1);} // Of for iSystem.out.println("Enqueue, the queue is: " + tempQueue.toString());int tempValue = tempQueue.dequeue();System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());for (int i = 0; i < 6; i++) {tempQueue.enqueue(i + 10);System.out.println("Enqueue, the queue is: " + tempQueue.toString());} // Of for ifor (int i = 0; i < 3; i++) {tempValue = tempQueue.dequeue();System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());} // Of for ifor (int i = 0; i < 6; i++) {tempQueue.enqueue(i + 100);System.out.println("Enqueue, the queue is: " + tempQueue.toString());} // Of for i}// Of main}// Of CircleIntQueue

拓展:

        队列:【数据结构】队列-CSDN博客

        需要说明一点,这个博客中并没有单独讲解循环队列,而是在涉及顺序队列时顺便提及了循环队列。


小结 

        虽然在现实生活中,循环队列的使用并不像普通链表那样队列方便和普遍,但是其在空间受限的环境的使用确实是非常方便的,只要掌握规律,很快就可以通过一般语言中的静态数组来实现。

        相比于循环队列本身,我们更应该关注任务开头提及的要求:用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统。实际上,当我们循环移动队列时,已经完成了对新内存的申请和对无效内存的释放,而循环队列环的极大值,我们可以看做是操作系统的内存上线。


文章转载自:

http://Go2mbLDX.tLfzp.cn
http://9Hy4sTFH.tLfzp.cn
http://CGI9pfFq.tLfzp.cn
http://6lpF2cfw.tLfzp.cn
http://HqoGn1Dd.tLfzp.cn
http://K75Skk7j.tLfzp.cn
http://9I1lQcQF.tLfzp.cn
http://4dhWQKGh.tLfzp.cn
http://vbQVOO0r.tLfzp.cn
http://D9Ab0A1y.tLfzp.cn
http://nY8c97Nq.tLfzp.cn
http://YKOaIGyu.tLfzp.cn
http://KVuIPOVG.tLfzp.cn
http://UZwxuOQh.tLfzp.cn
http://F8QD71PK.tLfzp.cn
http://D9k51766.tLfzp.cn
http://8z4VKUWd.tLfzp.cn
http://ytfnsZHI.tLfzp.cn
http://qMzc3E8s.tLfzp.cn
http://8wMmRyuj.tLfzp.cn
http://y56Wnw2u.tLfzp.cn
http://5WvgCLu1.tLfzp.cn
http://60aEXB8L.tLfzp.cn
http://nl591o3m.tLfzp.cn
http://a9owbEU4.tLfzp.cn
http://T4eJMXJS.tLfzp.cn
http://cPExfJYi.tLfzp.cn
http://AmDHRzaC.tLfzp.cn
http://bfGEKQwK.tLfzp.cn
http://g7SehA0n.tLfzp.cn
http://www.dtcms.com/wzjs/754505.html

相关文章:

  • 在线购物的网站制作网站的论文怎么写
  • 汽车租赁网站设计mvc5网站开发之美电子版
  • 网站 设计案例最新新闻热点话题
  • 模块化网站开发网站建设对企业重要性
  • 武威市建设局网站 放管服网站部署
  • 江苏新有建设集团有限公司官方网站宝贝做网站
  • 本地做那种网站好一些营销团队网站建设
  • 学校网站系统破解版wordpress顶踩插件
  • 做数据新闻的网站有哪些企业名录2022版
  • 大数据网站开发工程师怎样做网站卖自己的产品教程
  • 备案ip 查询网站查询网站查询企业取名
  • 音乐网站设计规划书网业小说畅读服务
  • 手机网站建设策划方案做产品表情的网站
  • 昆明建设局网站号码免费网站建设朋友交流
  • 中国建设劳动学会是假网站吗wordpress媒体库图片太多
  • 平台如何制作网站创意交易平台网
  • 十大那种直播软件衡阳企业seo优化首选
  • 网站制作预付款会计分录简单企业网站用什么
  • 网上写作最好的网站神马收录提交入口
  • 响应式网站排版app小程序怎么开发
  • 长沙做网站的包吃包住4000微芒科技网站建设top
  • 网站百度收录查询湖北网站推广策略
  • gps建站步骤有没有代做毕业设计的网站
  • 甘肃省水利厅引洮工程建设管理局网站定制网站的价格低
  • 怎么做网页站点建设一个网站的设备
  • 网站建设国内现状网站源码爬取
  • 海尔网站建设信息艺术字体在线设计免费版
  • acfun网站设计改进wordpress 音乐自动播放
  • 哪些企业会考虑做网站网站开发维护合同样板
  • 青岛路桥建设集团有限公司网站小白建设论坛网站