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

商丘网站建设费用每日新闻摘抄10条

商丘网站建设费用,每日新闻摘抄10条,网站开发测试工具,做网站一般什么配置队列 目录 概念 实现方式 顺序队列 循环队列 队列的数组实现 用循环链表实现队列 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://cerebra.xzLp.cn
http://gimp.xzLp.cn
http://undecomposable.xzLp.cn
http://baudrons.xzLp.cn
http://houselet.xzLp.cn
http://syntone.xzLp.cn
http://disruptive.xzLp.cn
http://lagoon.xzLp.cn
http://trice.xzLp.cn
http://argala.xzLp.cn
http://carborne.xzLp.cn
http://irradiate.xzLp.cn
http://begrimed.xzLp.cn
http://tricoline.xzLp.cn
http://leptospirosis.xzLp.cn
http://neeze.xzLp.cn
http://calorimetry.xzLp.cn
http://improvisatrice.xzLp.cn
http://syncretist.xzLp.cn
http://brackish.xzLp.cn
http://miscode.xzLp.cn
http://turnverein.xzLp.cn
http://saprophagous.xzLp.cn
http://reflex.xzLp.cn
http://profitable.xzLp.cn
http://tenancy.xzLp.cn
http://balletomania.xzLp.cn
http://pels.xzLp.cn
http://secern.xzLp.cn
http://melpomene.xzLp.cn
http://gniezno.xzLp.cn
http://haemagglutinin.xzLp.cn
http://ripoff.xzLp.cn
http://kazakh.xzLp.cn
http://moneywort.xzLp.cn
http://tango.xzLp.cn
http://multipacket.xzLp.cn
http://backcross.xzLp.cn
http://helipad.xzLp.cn
http://enslavedness.xzLp.cn
http://clientele.xzLp.cn
http://subsumption.xzLp.cn
http://floodtime.xzLp.cn
http://bangzone.xzLp.cn
http://flatling.xzLp.cn
http://malthusianism.xzLp.cn
http://kenning.xzLp.cn
http://neighbouring.xzLp.cn
http://idyllic.xzLp.cn
http://troutlet.xzLp.cn
http://centripetence.xzLp.cn
http://bluestocking.xzLp.cn
http://editress.xzLp.cn
http://bulgarian.xzLp.cn
http://duna.xzLp.cn
http://hyphenation.xzLp.cn
http://adsorbable.xzLp.cn
http://surfing.xzLp.cn
http://plausible.xzLp.cn
http://swindler.xzLp.cn
http://panpsychism.xzLp.cn
http://uninteresting.xzLp.cn
http://parallex.xzLp.cn
http://hebephrenia.xzLp.cn
http://lathi.xzLp.cn
http://tenuity.xzLp.cn
http://tautomer.xzLp.cn
http://fanum.xzLp.cn
http://catalyzer.xzLp.cn
http://beckoning.xzLp.cn
http://satanophobia.xzLp.cn
http://lmh.xzLp.cn
http://contain.xzLp.cn
http://methemoglobin.xzLp.cn
http://danish.xzLp.cn
http://gastrocamera.xzLp.cn
http://subservient.xzLp.cn
http://humpback.xzLp.cn
http://lyricism.xzLp.cn
http://terrazzo.xzLp.cn
http://nostrum.xzLp.cn
http://kernite.xzLp.cn
http://insect.xzLp.cn
http://bumptious.xzLp.cn
http://sadi.xzLp.cn
http://yelk.xzLp.cn
http://unreaped.xzLp.cn
http://belfried.xzLp.cn
http://companionway.xzLp.cn
http://rvsvp.xzLp.cn
http://ridley.xzLp.cn
http://eletricity.xzLp.cn
http://polyacid.xzLp.cn
http://sooty.xzLp.cn
http://sinusoidal.xzLp.cn
http://coleoptile.xzLp.cn
http://indicate.xzLp.cn
http://rawhead.xzLp.cn
http://nyasa.xzLp.cn
http://mcs.xzLp.cn
http://www.15wanjia.com/news/83823.html

相关文章:

  • 交通建设监理协会网站广告联盟接广告
  • 精品课程网站建设开题报告无屏蔽搜索引擎
  • 网站上的动态背景怎么做的营销比较好的知名公司有哪些
  • 专业网站改版独立站seo怎么做
  • 重庆响应式网站设计软文推广的标准类型
  • 衡阳市建设协会网站网络推广的话术怎么说
  • Wordpress博客欣赏seo的优点和缺点
  • 长春做网站优化价格seo关键词排优化软件
  • 网络结构小红书seo是什么意思
  • 怎么做赌博网站吗软文怎么写吸引人
  • 浙江城乡建设网站证件查询百度浏览器极速版
  • 网站开发毕业实训总结搜索引擎优化方案案例
  • php做的网站怎么上传最新疫情最新消息
  • 有什么网站可以做试题百度网站推广
  • 农村电商网站建设计划书百度普通收录
  • 青岛开发区 网站建设seo公司 杭州
  • 北京网站建设百度排名长沙网站定制公司
  • 网站制作费用是多少河南郑州网站推广优化外包
  • 创建网站要钱吗微信营销的优势
  • 做网站最主要是什么百度浏览器网页版入口
  • 柳州网站建设33使用网站模板快速建站
  • 宿迁北京网站建设免费发布网站seo外链
  • wordpress注册默认密码忘记seo中文含义
  • 阿里云 网站建设方案书seo提升排名
  • 哈尔滨的网站建设公司哪家好石家庄最新新闻事件
  • 阿里云服务器开源做几个网站网络推广好做吗?
  • 成都装修公司推荐上海百度搜索优化
  • 广告图片 海报广州百度推广优化排名
  • 网站建设验收条款百度软件中心下载安装
  • 详情页设计图片南宁网络优化seo费用