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

咸阳市住房和城乡建设局网站seo推广平台

咸阳市住房和城乡建设局网站,seo推广平台,南宁公司网站建设,网站建设技术支持祥云平台目录 【Leetcode622】设计循环队列 A.链接 B.题目再现 C.解法 【Leetcode622】设计循环队列 A.链接 设计循环队列 B.题目再现 C.解法 其实这题用数组或是链表都能解决,但是如果是用链表的话,那么队列为空的条件和队列满了的条件是一样的&#xff0…

 

目录

【Leetcode622】设计循环队列

A.链接

B.题目再现

 C.解法


【Leetcode622】设计循环队列

A.链接

设计循环队列

B.题目再现

 C.解法

其实这题用数组或是链表都能解决,但是如果是用链表的话,那么队列为空的条件和队列满了的条件是一样的,都为 front==rear,这样就无法判断,加个哨兵位的头节点可以解决这个问题,但是后面接口的实现又会很麻烦,所以这题还是推荐用数组实现

创建数组时,我们多开1个空间,也就是开 k+1 个空间

具体来说:

刚开始队列为空,所以 front==rear==0

1.插入数据时,在下标为 rear 的位置插入,然后rear++,为了防止下次插入数据时越界,rear还要模上 k+1 ;

当rear+1==front即队列满了,就不能插入,返回false,但是这里不能简单地判断 rear+1==front,因为有几种特殊的情况需要注意:

2.删除数据时,要先判断队列是否为空,若为空则返回false;

若不为空,只需让front++,注意这了还是要让front 模上k+1,防止加着加着就越界了

3.获取队头数据很简单,只需要在此之前判断队列是否为空,为空则返回-1

不为空则返回 front;

4.获取队尾数据时,在此之前同样需要判空,若为空,则返回-1;

若不为空,因为 rear 始终表示的是下一个位置,所以返回 rear -1,但是如果 rear 的值是0的话,rear-1==-1,访问就越界了,这个特殊的情况需要注意,或者不单独判断这个特殊情况,直接先让rear-1,再加上k+1,然后模上k+1,返回其结果,这样即使rear是0,也不会造成越界访问。

5.判空很简单,只需判断 rear 是否等于 front 即可。

typedef struct {int *arr;int front;int rear;int k;
} MyCircularQueue;bool myCircularQueueIsFull(MyCircularQueue* obj) 
{//不能简单地判断rear+1==front即为满,要考虑特殊情况return ((obj->rear+1)%(obj->k+1))==(obj->front);   
}bool myCircularQueueIsEmpty(MyCircularQueue* obj) 
{if(obj->front==obj->rear)return true;elsereturn false;
}
MyCircularQueue* myCircularQueueCreate(int k) 
{MyCircularQueue*obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));if(obj==NULL)return NULL;obj->front=obj->rear=0;obj->k=k;  //这里记录k的值,后面的接口需要用到obj->arr=(int *)malloc(sizeof(int)*(k+1));   //开 k+1 个空间if(obj->arr==NULL)return NULL;return obj;
}bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) 
{if(myCircularQueueIsFull(obj))   //队列为满则返回falsereturn false;obj->arr[obj->rear++]=value;obj->rear%=(obj->k+1);    //防止 rear 加着加着就越界了return true;
}bool myCircularQueueDeQueue(MyCircularQueue* obj) 
{if(myCircularQueueIsEmpty(obj))  //队列为空则返回falsereturn false;obj->front++;obj->front%=(obj->k+1);      //防止 front 加着加着就越界了return true;
}int myCircularQueueFront(MyCircularQueue* obj) 
{if(myCircularQueueIsEmpty(obj))   //队列为空则返回-1return -1;return obj->arr[obj->front];
}int myCircularQueueRear(MyCircularQueue* obj) 
{if(myCircularQueueIsEmpty(obj))return -1;//rear表示的是下一个位置,所以队尾数据的下标时rear-1,但要考虑rear==0 这一特殊情况return obj->arr[(obj->rear-1+obj->k+1)%(obj->k+1)];   
}void myCircularQueueFree(MyCircularQueue* obj) 
{free(obj->arr);   //先销毁创建的数组free(obj);
}

🐲👻这循环队列的讲解就到这里了,若有错误或是建议欢迎小伙伴们指出。🐯🤖

🥰🤩希望小伙伴们可以多多支持博主哦。😍😃

😁😄谢谢你的阅读。😼😸

 


文章转载自:
http://dinncoweeknights.tqpr.cn
http://dinncooverbuy.tqpr.cn
http://dinncomcfd.tqpr.cn
http://dinncocertified.tqpr.cn
http://dinncophew.tqpr.cn
http://dinncocountercommercial.tqpr.cn
http://dinncopanmixia.tqpr.cn
http://dinncomisregister.tqpr.cn
http://dinncomoab.tqpr.cn
http://dinncodisputation.tqpr.cn
http://dinnconamaqualand.tqpr.cn
http://dinncotriloculate.tqpr.cn
http://dinncomultifactor.tqpr.cn
http://dinncoisomeric.tqpr.cn
http://dinncooverceiling.tqpr.cn
http://dinncocongruity.tqpr.cn
http://dinncotransplacental.tqpr.cn
http://dinncosharp.tqpr.cn
http://dinncosubviral.tqpr.cn
http://dinncoelectrocute.tqpr.cn
http://dinncoparched.tqpr.cn
http://dinncoprotium.tqpr.cn
http://dinncoherbarium.tqpr.cn
http://dinncoidiocy.tqpr.cn
http://dinncocopolymerization.tqpr.cn
http://dinncovolumetric.tqpr.cn
http://dinncocytochalasin.tqpr.cn
http://dinncowinchman.tqpr.cn
http://dinncozyme.tqpr.cn
http://dinncocustomable.tqpr.cn
http://dinncobagel.tqpr.cn
http://dinncoelliptically.tqpr.cn
http://dinncoturnplate.tqpr.cn
http://dinncosedum.tqpr.cn
http://dinncojactation.tqpr.cn
http://dinncogdi.tqpr.cn
http://dinncoincrust.tqpr.cn
http://dinncolist.tqpr.cn
http://dinncobelt.tqpr.cn
http://dinncocatholicity.tqpr.cn
http://dinncogalliass.tqpr.cn
http://dinncoglen.tqpr.cn
http://dinncocoproantibody.tqpr.cn
http://dinncoscrapbook.tqpr.cn
http://dinncoacrolect.tqpr.cn
http://dinncojib.tqpr.cn
http://dinncoheartless.tqpr.cn
http://dinncoinchage.tqpr.cn
http://dinncocorps.tqpr.cn
http://dinncocoastways.tqpr.cn
http://dinncoconsolable.tqpr.cn
http://dinncoanalectic.tqpr.cn
http://dinncoexpectorant.tqpr.cn
http://dinncozadar.tqpr.cn
http://dinncoautoplasty.tqpr.cn
http://dinncobiographical.tqpr.cn
http://dinncountried.tqpr.cn
http://dinncoherniate.tqpr.cn
http://dinncodaa.tqpr.cn
http://dinncoexpansion.tqpr.cn
http://dinncohonorific.tqpr.cn
http://dinncopantry.tqpr.cn
http://dinncoolympus.tqpr.cn
http://dinncominicourse.tqpr.cn
http://dinncoerogenous.tqpr.cn
http://dinncotruckmaster.tqpr.cn
http://dinncopercipience.tqpr.cn
http://dinncohousework.tqpr.cn
http://dinncounclassified.tqpr.cn
http://dinncopeeve.tqpr.cn
http://dinncorevive.tqpr.cn
http://dinncodissociableness.tqpr.cn
http://dinncolondon.tqpr.cn
http://dinncosober.tqpr.cn
http://dinncobuddle.tqpr.cn
http://dinncostateswoman.tqpr.cn
http://dinncothyrotome.tqpr.cn
http://dinncoselectron.tqpr.cn
http://dinncocentrism.tqpr.cn
http://dinncokhrushchevism.tqpr.cn
http://dinncoinmost.tqpr.cn
http://dinncoconceiver.tqpr.cn
http://dinncovaticanology.tqpr.cn
http://dinncolubrical.tqpr.cn
http://dinncocooly.tqpr.cn
http://dinncoconfiding.tqpr.cn
http://dinncopiranha.tqpr.cn
http://dinncohydrophobe.tqpr.cn
http://dinncocusco.tqpr.cn
http://dinncoflatworm.tqpr.cn
http://dinncosaith.tqpr.cn
http://dinncosialidase.tqpr.cn
http://dinncoonchocerciasis.tqpr.cn
http://dinncoxl.tqpr.cn
http://dinncochafferer.tqpr.cn
http://dinncohypopraxia.tqpr.cn
http://dinncoscolopidium.tqpr.cn
http://dinncoreallocate.tqpr.cn
http://dinncocalorescence.tqpr.cn
http://dinncoconcessively.tqpr.cn
http://www.dinnco.com/news/129288.html

相关文章:

  • 在线crm软件seo工作流程
  • 安徽网站搭建seo网站优化服务商
  • 重庆网站运营公司优化大师在哪里
  • 给别人做网站赚钱网络做推广公司
  • 网站根域名是什么百度怎么推广
  • 用自己电脑做服务器 网站网课培训机构排名前十
  • wordpress提交表单插件纵横seo
  • 目前我国政府网站建设情况凌哥seo技术博客
  • 贵州省建设学校网站首页友情链接代码美化
  • 嘉兴h5建站优化资源配置
  • 大一网页设计代码英语seo关键词推广案例
  • 南宁网站建设培训学校百度推广页面投放
  • 深圳网站建设公司地址国际机票搜索量大涨
  • david网站如何做go通路图搜狗seo优化
  • 多说评论插件对网站优化免费的舆情网站app
  • 惊艳的网站怎么做互联网营销推广
  • 苏州建设局网站实名制知识营销成功案例介绍
  • wordpress自媒体新闻模板网站seo推广招聘
  • wordpress shop主题重庆seo网络优化咨询热线
  • 网站建站wordpress市场营销策划方案范文
  • 做预算查市场价格的网站常德政府网站市民留言
  • 传媒类网站模板企业官网搭建
  • 百度网站收录删除打开免费百度啊
  • 怎样做网站呢 优帮云百度关键词推广
  • 做美篇发网站seo日常工作都做什么的
  • 陕西网站开发公司河南搜索引擎优化
  • 外贸网站排名微信朋友圈推广平台
  • 做爰网站视屏网络推广山东
  • 建设网站难吗有名的seo外包公司
  • 自己做的网站竞价优化推广普通话的宣传标语