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

怎么给客户谈做网站公司网站模版

怎么给客户谈做网站,公司网站模版,dw怎样做收藏本网站,青羊网站建设目录 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://www.cadmedia.cn/news/6970.html

相关文章:

  • 深圳网站设计公司招聘淘宝推广平台
  • 西宁集团网站建设荥阳网站优化公司
  • 手机网站 像素企业关键词优化最新报价
  • 北京海淀公司网站icp备案发布悬赏任务的推广平台
  • 独立做网站需要学什么网络营销技巧
  • 免费域名证书申请seo网站系统
  • 通信工程网站建设企业网络营销策划方案
  • 信息公开网站建设seo 网站优化推广排名教程
  • 有域名怎么免费建站网店网络推广方案
  • 甘肃定西校园文化设计公司seo专业培训中心
  • 2022全国封城名单怎么优化百度关键词
  • seo的形式有哪些搜狗关键词优化软件
  • 网站备案成功后怎么弄今日实时热搜
  • 郑州网站设计多少钱下载百度app下载
  • 北京 网站开发做网络推广有哪些平台
  • 一个设计网站多少钱seo教程视频
  • 网站内外链建设手机端关键词排名免费软件
  • 建筑资料免费下载网站抖音搜索seo排名优化
  • 制作企业网站步骤网络营销的新特点
  • 家居企业网站建设方案百度指数怎么看
  • 金融行业网站建设郑州做网站公司排名
  • 网站策划人员需要做哪些工作抖音矩阵排名软件seo
  • 住房和城乡建设行业证书seo是什么职业做什么的
  • 建设执业资格管理中心网站中级经济师考试
  • 从化高端网站建设搜索引擎下载
  • 广告设计公司介绍范文电子商务沙盘seo关键词
  • 专业装修设计网站成都网站推广哪家专业
  • 怎样做美瞳网站网络销售挣钱吗
  • 科技打破垄断全球的霸权好看的seo网站
  • 泗水网站建设ys178soso搜索引擎