栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构循环队列的插入和删除练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构循环队列的插入和删除练习

#include 去掉了
using namespace std;
//#include
//#include
#define MAXSIZE 5//只能实现四个元素插入
//不能因为下面的定义认为是
// a[0],a[1],a[2],a[3],a[4],a[5],事实上只到a[4]就结束了
//循环队列C++实现
//C语言只需要修改头文件以及cout改成printf即可
typedef struct queue {
int data[MAXSIZE];
int front;
int rear;//队尾指针不断变化
}queue;
//队列最后一个元素不写数据,用来判断满或空
//队空:front= =rear
//队满:(rear+1)%MAXSIZE==front
queue* init() {
queue* L = (queue*)malloc(sizeof(queue));
L->front = L->rear = 0;
return L;
}
int judge(queue* L) {
if (L->front == L->rear) //空
return 0;
else
if ((L->rear + 1) % MAXSIZE == L->front)
return 1;//队满
else
return -1;//非空非满
}
//FIFO
//插入元素用尾插法即队尾指针一直在移动,队首指针不变
//仅当删除时队首指针开始移动,队尾不变
void insQueue(queue* L, int data) {
if (judge(L) == 0 || judge(L) == -1) {
L->data[L->rear] = data;
int zj = L->data[L->rear];
L->rear = (L->rear + 1) % MAXSIZE;
cout << “元素:” << zj << “插入成功” << endl;
}
else {
int zj = data;
cout << “队满,” < }
}
void outQueue(queue* L) {
if (judge(L) == 0) cout<<“队空”< else {
int data = L->data[L->front];
L->front = (L->front + 1) % MAXSIZE;
cout<<“出队的数是:”<< data< }
}
void show(queue* L) {
if (judge(L) == 0) cout<<“队空”< else{
int length = (L->rear - L->front + MAXSIZE) % MAXSIZE;
int i = 0, zj = L->front;//保存队首
do {
cout << L->data[zj]<<"->";
zj = (zj+1)%MAXSIZE;
} while (length>++i);
}
printf(“NULLn”);
}
int main() {
queue* L = init();
insQueue(L, 1);
insQueue(L, 3);
insQueue(L, 5);
insQueue(L, 7);
insQueue(L, 9);
show(L);
cout << “* * * * * * * * * * * * " < outQueue(L);
show(L);
cout << "
* * * * * * * * * * *" < outQueue(L);
show(L);
cout<<"
* * * * * * * * * ***” < return 0;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号