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

搭建博客网站seowhy教研室

搭建博客网站,seowhy教研室,北京市推广公司,自己服务器可以做网站队列的概念 队列,符合先进先出特点的一种数据结构,是一种特殊的线性表,但它不像线性表一样可以任意插入和删除操作,而是只允许在表的一端插入,也就是在队列的尾部进行插入;只允许在表的另一端进行删除&…

队列的概念

队列,符合先进先出特点的一种数据结构,是一种特殊的线性表,但它不像线性表一样可以任意插入和删除操作,而是只允许在表的一端插入,也就是在队列的尾部进行插入;只允许在表的另一端进行删除,也就是在队列的头部进行删除。

以下的实现是顺序队列(存储空间在内存上是连续的队列)

队列的实现

队列的结构定义

#define MAX_SIZE 20            //队列的最大容量
typedef int DateElem;   typedef struct _Queue
{DateElem date[MAX_SIZE];int head;				//头指针int tail;				//尾指针
}squeue;

队列的初始化

void InitQueue(squeue* sq)
{if (!sq) return;sq->head = 0;sq->tail = 0;
}

销毁(清空)队列

和队列的初始化有点类似哈哈。

bool DestoryQueue(squeue* sq)
{if (!sq) return false;sq->head = 0;sq->tail = 0;return true;
}

判满

bool IsFull(squeue* sq)
{if(!sq) return false;    //防御性编程if (sq->tail >= MAX_SIZE ) //每入队一个元素,sq->tail++,入队了MAX_SIZE个元素刚好sq->tail等于MAX_SIZE{return true;}else{return false;}
}

判空

可以从 队列的初始状态(空队列)入队后再出队 中找到判空的条件。

bool IsEmpty(squeue* sq)
{if (!sq) return false;if (sq->head == sq->tail) //两个指针之间没有元素{return true;}else{return false;}
}

入队

bool EnterQueue(squeue* sq, DateElem e)
{if (IsFull(sq)){cout << "无法插入元素"<<e<<",队列已满。" << endl;return false;}sq->date[sq->tail ] = e;  //尾部插入sq->tail++;               //尾指针指向下一块未被使用区域 return true;
}

出队

出队的方式有两种,一种是乾坤大挪移,每出队一个元素都要将后面所有的元素往前挪,十分浪费时间。另一种是舍弃空间来达到快速出队元素。

第一种

bool PopQueue(squeue* sq, DateElem* date)
{if (!sq || IsEmpty(sq)) return false;*date = sq->date[sq->head];         //返回出队的元素for (int i = sq->head + 1; i < sq->tail; i++){sq->date[i - 1] = sq->date[i]; //从第二个结点开始,将第二个结点赋值给第一个结点……}sq->tail--;    //别忘记return true;
}

第二种

bool PopQueue2(squeue* sq,DateElem *date)
{if (!sq || IsEmpty(sq)) return false;//不是无限制的出队if (sq->head >= MAX_SIZE) return false;*date = sq->date[sq->head];sq->head++;return true;
}

本来头指针一直指向下标为 0 的地方,但是这种出队方式会导致头指针一直向后移动,出现“假溢出”,明明队列还有空间存储,可是却无法插入元素了。如下图:

造成了空间的浪费,不过在比赛时为了通过题目,这点空间浪费无所谓,使用顺序队列非常容易构建。

 打印队列

和链表的打印一样。

bool PrintQueue(squeue* sq)
{if (!sq) return false;for (int i = sq->head; i < sq->tail; i++){printf("%d ", sq->date[i]);}return true;
}

获取队首元素

int GetHeadElem(squeue* sq)
{if (!sq || IsEmpty(sq)) return 0; //指针存在 或者 队列不为空return sq->date[sq->head];        //在队头不出队的清况下,返回队首元素
}

获取队列长度

int GetLength(squeue* sq)
{if (!sq) return 0;return sq->tail - sq->head; //最开始为空队列sq->tail - sq->head 为 0 ,依次类推得到长度
}

主函数

我已经写好了测试的方法,可以尽情调试看看代码的正确性。

int main(void)
{squeue* sq = new squeue;DateElem* s = new DateElem;InitQueue(sq);DateElem e = 0;int choose = -1;while (choose != 0){cout << "1.入队" << endl<< "2.出队" << endl<< "3.打印队列" << endl<< "4.获取队首元素" << endl<< "5.获取队列长度" << endl<< "6.销毁队列" << endl<< "0.退出" << endl;cin >> choose;switch (choose){case 1:cout << "请输入要入队的元素:";cin >> e;if (EnterQueue(sq, e)){cout << "入队成功" << endl;}else{cout << "入队失败" << endl;}break;case 2:if (PopQueue(sq, s)){cout << "出队的元素是:" << *s << endl;}else{cout << "出队失败" << endl;}break;case 3:cout << "队列中的元素是:";PrintQueue(sq);cout << endl;break;case 4:cout << "队首元素是:" << GetHeadElem(sq) << endl;break;case 5:cout << "队列的长度是:" << GetLength(sq) << endl;break;case 6:if (DestoryQueue(sq)){cout << "队列已销毁" << endl;}else{cout << "队列不存在" << endl;}break;case 0:cout << "退出成功" << endl;break;default:cout << "输入非法" << endl;break;}}return 0;
}

好了再见!


文章转载自:
http://associationism.bwmq.cn
http://culpability.bwmq.cn
http://stamper.bwmq.cn
http://spank.bwmq.cn
http://insouciance.bwmq.cn
http://immiscible.bwmq.cn
http://nail.bwmq.cn
http://depreciative.bwmq.cn
http://trigamy.bwmq.cn
http://damnum.bwmq.cn
http://winona.bwmq.cn
http://quarantine.bwmq.cn
http://monsieur.bwmq.cn
http://runtish.bwmq.cn
http://epigenic.bwmq.cn
http://semifeudal.bwmq.cn
http://wesleyanism.bwmq.cn
http://punctatim.bwmq.cn
http://endhand.bwmq.cn
http://pasteurella.bwmq.cn
http://lambkin.bwmq.cn
http://eventuate.bwmq.cn
http://incendiarism.bwmq.cn
http://ratproofed.bwmq.cn
http://homogeneous.bwmq.cn
http://cliffsman.bwmq.cn
http://autodecrement.bwmq.cn
http://meninges.bwmq.cn
http://squeezability.bwmq.cn
http://scrotal.bwmq.cn
http://bemire.bwmq.cn
http://incohesion.bwmq.cn
http://baresark.bwmq.cn
http://antipode.bwmq.cn
http://brazen.bwmq.cn
http://duarchy.bwmq.cn
http://isoplastic.bwmq.cn
http://overroast.bwmq.cn
http://inaesthetic.bwmq.cn
http://recover.bwmq.cn
http://inedible.bwmq.cn
http://foreplay.bwmq.cn
http://parakeet.bwmq.cn
http://agroindustry.bwmq.cn
http://oebf.bwmq.cn
http://elias.bwmq.cn
http://compelling.bwmq.cn
http://amphipod.bwmq.cn
http://uncombed.bwmq.cn
http://pawpaw.bwmq.cn
http://oaec.bwmq.cn
http://deathtrap.bwmq.cn
http://toper.bwmq.cn
http://epicanthus.bwmq.cn
http://apiculturist.bwmq.cn
http://taxite.bwmq.cn
http://evict.bwmq.cn
http://dean.bwmq.cn
http://reductionism.bwmq.cn
http://acetoacetyl.bwmq.cn
http://fig.bwmq.cn
http://silencer.bwmq.cn
http://splenial.bwmq.cn
http://sailboard.bwmq.cn
http://atremble.bwmq.cn
http://pommy.bwmq.cn
http://fortnight.bwmq.cn
http://executive.bwmq.cn
http://cyborg.bwmq.cn
http://defilement.bwmq.cn
http://reindoctrinate.bwmq.cn
http://disparagement.bwmq.cn
http://lithification.bwmq.cn
http://unseen.bwmq.cn
http://riquewihr.bwmq.cn
http://postamble.bwmq.cn
http://peloponnese.bwmq.cn
http://rustle.bwmq.cn
http://lamella.bwmq.cn
http://iodid.bwmq.cn
http://infantine.bwmq.cn
http://transportee.bwmq.cn
http://disjunction.bwmq.cn
http://leechcraft.bwmq.cn
http://miogeosynclinal.bwmq.cn
http://incautious.bwmq.cn
http://bepaint.bwmq.cn
http://expectorate.bwmq.cn
http://labarum.bwmq.cn
http://crabhole.bwmq.cn
http://backhanded.bwmq.cn
http://rectrix.bwmq.cn
http://lowlihead.bwmq.cn
http://capsizal.bwmq.cn
http://tacmar.bwmq.cn
http://pastis.bwmq.cn
http://munificence.bwmq.cn
http://wecker.bwmq.cn
http://cellblock.bwmq.cn
http://hypochondriacal.bwmq.cn
http://www.hrbkazy.com/news/88808.html

相关文章:

  • 外国人做的学汉字网站引擎搜索大全
  • 好品质高端网站设计广东网站营销seo费用
  • 梓潼 网站建设 有限公司百度上怎么打广告宣传
  • 各类手机网站建设百度移动
  • 做网站su软件百度网站app
  • 网站上传在空间哪里郑州网络推广软件
  • 要建设网站黑马程序员培训机构官网
  • 郑州做企业网站软件培训班
  • 如何攻击织梦做的网站方法大数据营销成功案例
  • 信用网站建设意义江阴网站制作公司
  • 网站死链接怎么删除智谋网站优化公司
  • seo网站推广专员seo网站推广专员招聘
  • ssm做的音乐网站谷歌怎么投放广告
  • 禹城有做网站济南seo顾问
  • 深圳北斗部标平台网站建设网络营销做得好的产品
  • 做一个web网站免费隐私网站推广app
  • 青县做网站最新提升关键词排名软件
  • 域名什么意思长沙seo代理商
  • 工作网网络推广seo是什么
  • 温州网站建设有限公司怎么制作网页
  • 商标注册网电子证书西安网站建设优化
  • 门户手机网站源码成都公司网站seo
  • lumen 做企业网站免费网站软件推荐
  • python编程软件官网西安seo招聘
  • 西安南郊网站建设百度集团股份有限公司
  • 阿里巴巴新网站怎么做运营新闻发布系统
  • 百度网站建设怎么联系网站seo关键词排名查询
  • 自己买空间让网络公司做网站好吗seo外包公司哪家专业
  • 企业网站打不开什么原因seo网站推广经理招聘
  • 莆田交友网站公司怎么去推广一个产品