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

商丘网站建设费用滕州今日头条新闻

商丘网站建设费用,滕州今日头条新闻,公司做网站是做什么账务处理,wordpress信息发布队列 目录 概念 实现方式 顺序队列 循环队列 队列的数组实现 用循环链表实现队列 STL 之 queue 实现队列 STL 之 dequeue 实现双端队列 概念 队列是一种特殊的线性表,它只允许在表的前端(称为队头,front)进行删除操作…

队列

目录

概念

实现方式

顺序队列

循环队列

队列的数组实现

用循环链表实现队列

STL 之 queue 实现队列 

STL 之 dequeue 实现双端队列 


概念

队列是一种特殊的线性表,它只允许在表的前端(称为队头,front)进行删除操作——出队,而在表的后端(称为队尾,rear)进行插入操作——入队,是一种操作受限的线性表。

最先进入队列的元素最先被删除,是“先进先出”的线性表。

实现方式

  • 数组
  • 链表
  • C++ 中可以使用 STL 中的 queue 实现
  • 其中 STL 中还包括双端队列 dequeue

顺序队列

必须静态分配或者动态申请空间,并设置两个指针管理,一个是队头指针 front(指向队头元素),另一个是队尾指针 rear(指向下一个入队元素的存储位置)。

队空的判断条件是:front==rear,队满的条件为:rear==MAXQSIZE。

循环队列

为了使队列空间可以重复使用,可以对循环队列进行改进,无论是插入或者删除,一旦 rear 指针或者 front 指针超出了所分配的队列空间,就让它指向这篇连续空间的起始位置,自己从 MAXQSIZE 增 1 变为 1,可以用取余运算实现: (rear+1)%MAXQSIZE、 (front+1)%MAXQSIZE.

其中队空的判断条件为:front==rear,队满的条件为:(rear+1)%MAXQSIZE==front。

队列的数组实现

定义一个结构体实现队列:

struct node {int* data;int front;int rear;
};

初始化队列:(将队头和队尾指针都赋为 1),数组长度为 MAXQSIZE,开辟一块空间为 MAXQSIZE-1 的数组:

//为队列开辟空间并初始化(也可以在结构体中定义int data[MAXQSIZE])
void InitQueue(struct node& Q) {Q.rear = 1;Q.front = 1;Q.data = (int*)malloc(MAXQSIZE+1 * sizeof(int));
}

入队:

//入队
int EnQueue(struct node &Q,int y) {if (Q.rear != MAXQSIZE) {Q.data[Q.rear] = y;Q.rear++;return 1;}return 0;
}

出队:

//出队
int DeQueue(struct node& Q, int &y) {if (Q.rear != Q.front){y = Q.data[Q.front];Q.front++;return 1;}return 0;
}

总代码为:

//数组实现队列
#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100
int queue[MAXQSIZE];
struct node {int* data;int front;int rear;
};
//为队列开辟空间并初始化(也可以在结构体中定义int data[MAXQSIZE])
void InitQueue(struct node& Q) {Q.rear = 1;Q.front = 1;Q.data = (int*)malloc(MAXQSIZE+1 * sizeof(int));
}
//入队
int EnQueue(struct node &Q,int y) {if (Q.rear != MAXQSIZE) {Q.data[Q.rear] = y;Q.rear++;return 1;}return 0;
}
//出队
int DeQueue(struct node& Q, int &y) {if (Q.rear != Q.front){y = Q.data[Q.front];Q.front++;return 1;}return 0;
}
int main() {int n;int x, y;struct node Q;InitQueue(Q);//以1开头为插入(插入操作输入两个数),以0开头为删除printf("输入操作个数:(以1开头为插入(插入操作输入两个数),以0开头为删除)\n");scanf("%d", &n);while (n--) {printf("输入操作:");scanf("%d", &x);if (x == 1){scanf("%d", &y);if (EnQueue(Q, y))printf("入队成功\n");elseprintf("入队失败\n");}else if (x == 0){if (DeQueue(Q, y) == 0)printf("出队失败\n");elseprintf("出队的元素为:%d\n",y);}else{printf("输入正确的操作\n");n++;}}return 0;
}

用循环链表实现队列

不用循环时将初始化时的 Q->next=Q 删去。

以带头结点的循环链表表示队列,并且只设一个指针指向队尾结点,不设头指针。

定义一个结构体:

struct node {int data;struct node* next;
};

入队:

//入队
void EnQueue(struct node* Q, int y) {struct node* p;p = (struct node*)malloc(sizeof(struct node));p->data = y;p->next = Q->next;Q->next = p;Q = p;
}

出队:

//出队
int DeQueue(struct node* Q, int& y) {if (Q->next == Q)return 0;struct node* p;p = Q->next;y = p->data;Q->next = p->next;free(p);return 1;
}

总代码:

//以带头结点的循环链表表示队列,
//并且只设一个指针指向队尾结点,不设头指针。
#include<stdio.h>
#include<stdlib.h>
struct node {int data;struct node* next;
};
//循环队列的初始化
void InitQueue(struct node* Q) {Q->next = Q;//初始化循环队列
}
//入队
void EnQueue(struct node* Q, int y) {struct node* p;p = (struct node*)malloc(sizeof(struct node));p->data = y;p->next = Q->next;Q->next = p;Q = p;
}
//出队
int DeQueue(struct node* Q, int& y) {if (Q->next == Q)return 0;struct node* p;p = Q->next;y = p->data;Q->next = p->next;free(p);return 1;
}
int main() {struct node* Q;Q = (struct node*)malloc(sizeof(struct node));InitQueue(Q);printf("输入操作个数:(以1开头为插入(插入操作输入两个数),以0开头为删除)\n");int x, y, n;scanf("%d", &n);while(n--){printf("输入操作:");scanf("%d", &x);if(x==1){scanf("%d", &y);EnQueue(Q, y);printf("入队成功\n");}else if(x==0){if (DeQueue(Q, y) == 1)printf("%d\n", y);elseprintf("出队失败\n");}}return 0;
}

STL 之 queue 实现队列 

要加上头文件:#include<queue>

对应的函数:

构造空队列:

queue<int>q;

总代码: 

//用queue函数实现队列
#include<iostream>
#include<queue>
using namespace std;
int main() {queue<int>q;int n;int x, y;printf("输入操作个数:");scanf("%d", &n);printf("以1开头为插入(插入操作输入两个数),以0开头为删除\n");while (n--) {scanf("%d", &x);if (x == 1) {scanf("%d", &y);q.push(y);}else if(x==0){if (!q.empty()) {//判断队列不为空printf("%d\n", q.front());q.pop();}elseprintf("出队失败\n");}else{printf("输入正确的操作\n");n++;}printf("队列中元素个数为:%d\n", q.size());}return 0;
}

STL 之 dequeue 实现双端队列 

//用dequeue实现双端队列
#include<iostream>
#include<deque>
using namespace std;
struct node {int *data;
}Q;
int main() {deque<int>q(10);deque<int>::iterator idex;for (int i = 0; i < 10; i++) {q[i] = i;}for (int i = 0; i < 10; i++) {printf("%d ", q[i]);}printf("\n");//在头尾加入新元素printf("加入新元素后:\n");q.push_back(100);//加入队尾q.push_front(10);//加入队头printf("输出deque的数据:\n");for (idex = q.begin(); idex != q.end(); idex++) {printf("%d ", *idex);}printf("\n");//查找int x = 5;idex = find(q.begin(), q.end(), x);if (idex != q.end())printf("找到%d元素\n",x);elseprintf("队列中没有%d元素\n",x);//在头尾删除数据q.pop_back();//删除队尾q.pop_front();//删除队头printf("输出deque的数据:\n");for (idex = q.begin(); idex != q.end(); idex++) {printf("%d ", *idex);}return 0;
}


文章转载自:
http://smokeable.rmyt.cn
http://bronchogenic.rmyt.cn
http://darkroom.rmyt.cn
http://stagehand.rmyt.cn
http://overweary.rmyt.cn
http://distent.rmyt.cn
http://aptitudinal.rmyt.cn
http://cryptography.rmyt.cn
http://ungrounded.rmyt.cn
http://megascopic.rmyt.cn
http://spermagonium.rmyt.cn
http://animalize.rmyt.cn
http://tetrafluoride.rmyt.cn
http://strombuliform.rmyt.cn
http://zonerefine.rmyt.cn
http://completion.rmyt.cn
http://galatine.rmyt.cn
http://blackhearted.rmyt.cn
http://transistor.rmyt.cn
http://sermonize.rmyt.cn
http://galvanoplastics.rmyt.cn
http://fauna.rmyt.cn
http://aphelion.rmyt.cn
http://scotch.rmyt.cn
http://alkali.rmyt.cn
http://junto.rmyt.cn
http://oxytocic.rmyt.cn
http://gymnorhinal.rmyt.cn
http://neocosmic.rmyt.cn
http://bratty.rmyt.cn
http://sittwe.rmyt.cn
http://omen.rmyt.cn
http://robinsonite.rmyt.cn
http://phlebotomy.rmyt.cn
http://chiefdom.rmyt.cn
http://worldwide.rmyt.cn
http://qiviut.rmyt.cn
http://overrepresent.rmyt.cn
http://wolverine.rmyt.cn
http://raphis.rmyt.cn
http://flam.rmyt.cn
http://nonparametric.rmyt.cn
http://arytenoidectomy.rmyt.cn
http://signwriter.rmyt.cn
http://copulative.rmyt.cn
http://entelechy.rmyt.cn
http://lamina.rmyt.cn
http://widgeon.rmyt.cn
http://tidiness.rmyt.cn
http://photosynthesis.rmyt.cn
http://cobelligerent.rmyt.cn
http://relieving.rmyt.cn
http://inerrant.rmyt.cn
http://cocci.rmyt.cn
http://prase.rmyt.cn
http://macroptic.rmyt.cn
http://omt.rmyt.cn
http://salvoconducto.rmyt.cn
http://nyet.rmyt.cn
http://anomaloscope.rmyt.cn
http://accessing.rmyt.cn
http://emblematise.rmyt.cn
http://anhematopoiesis.rmyt.cn
http://sphygmogram.rmyt.cn
http://samoyedic.rmyt.cn
http://hypermetropia.rmyt.cn
http://ursprache.rmyt.cn
http://testacean.rmyt.cn
http://dacoit.rmyt.cn
http://bulgaria.rmyt.cn
http://calced.rmyt.cn
http://fortieth.rmyt.cn
http://arrogation.rmyt.cn
http://untenable.rmyt.cn
http://turnsole.rmyt.cn
http://managerialist.rmyt.cn
http://conferment.rmyt.cn
http://delustering.rmyt.cn
http://altercate.rmyt.cn
http://phenacetin.rmyt.cn
http://fallibly.rmyt.cn
http://bossdom.rmyt.cn
http://panther.rmyt.cn
http://algoid.rmyt.cn
http://codswallop.rmyt.cn
http://papeterie.rmyt.cn
http://unexpired.rmyt.cn
http://fop.rmyt.cn
http://encyclopaedia.rmyt.cn
http://lightsome.rmyt.cn
http://jugular.rmyt.cn
http://bluetongue.rmyt.cn
http://vagabondize.rmyt.cn
http://municipalize.rmyt.cn
http://dragon.rmyt.cn
http://yh.rmyt.cn
http://diminishingly.rmyt.cn
http://nephropexia.rmyt.cn
http://snowsuit.rmyt.cn
http://klatch.rmyt.cn
http://www.dt0577.cn/news/101724.html

相关文章:

  • 潍坊做网站建设的公司视频推广方案模板
  • 做网站的费用如何入账全网推广怎么做
  • 北京百度糯米团购有做网站的电话吗什么网站都能打开的浏览器
  • 做淘宝素材网站哪个好用百度信息
  • 郑州网站app开发的站长平台网站
  • 都有哪些网站可以做推广做网站优化哪家公司好
  • 购物网站成功案例seo关键词优化软件怎么样
  • 官方网站找oem做洗发水厂家磁力宅在线搜种子
  • 嘉兴网站快照优化公司seo信息网
  • 做网站的网址怎么弄免费的网站域名查询app
  • 汽车网站怎么做江苏网络推广公司
  • 厦门专业网站设计代理软件网站排行榜
  • dede英文网站国家反诈中心app下载
  • 网站图片有什么要求吗百度一下百度网站
  • 网站可以做2个公司的吗百度正版下载恢复百度
  • wordpress playyo郑州seo阿伟
  • 邢台提供网站建设公司电话免费站长统计工具
  • html5如何实现网站开发百度指数怎么查
  • jsp网站开发论文2017南召seo快速排名价格
  • 自助建站软件下载网络营销的十大特点
  • 企业没有网站怎么做seo优化网络营销策划推广公司
  • 郑州公司网站制作站长之家源码
  • 做网站最专业的公司河北seo平台
  • 整形医院网站建设推广百度极速版下载
  • 昆明app开发公司长治网站seo
  • asp学习网站手机百度推广怎么打广告
  • 合肥网站建设是什么网址ip地址查询工具
  • 做融资的网站有哪些最近国际新闻大事
  • 纸做的花朵成品网站seo社区
  • 深圳信科网站建设沈阳cms模板建站