数据结构与算法学习笔记三---循环队列的表示和实现(C语言)

devtools/2024/10/20 10:22:15/

目录

前言

1.为啥要使用循环队列

2.队列的顺序表示和实现

1.定义

2.初始化

3.销毁

4.清空

5.空队列

6.队列长度

7.获取队头

8.入队

9.出队

 10.遍历队列

11.完整代码


前言

    本篇博客介绍栈和队列的表示和实现。

1.为啥要使用循环队列

    上篇文章中我们知道了顺序队列的用法,但是顺序队列有个缺点就是会“假溢出”,浪费大量的存储空间,关于假溢出的问题,个人感觉数据结构里里面的这段解释比较好,就直接截图放下面了,大家自行阅读吧。

图1.顺序队列假溢出的问题

2.队列的顺序表示和实现

1.定义

#define MAX_QUEUE_SIZE 100 // 循环队列的最大容量typedef int Status;
typedef int ElemType;typedef struct {ElemType *data; // 存储数据的数组int front;      // 头指针,指向队首元素int rear;       // 尾指针,指向队尾元素的下一个位置int maxSize;    // 循环队列的最大容量
} CircularQueue;

2.初始化

        队列初始化的时候,队头和队尾指针均为0

// 初始化循环队列
Status initCircularQueue(CircularQueue *queue, int maxSize) {queue->data = (ElemType *)malloc(sizeof(ElemType) * maxSize);if (!queue->data) {return 0; // 内存分配失败}queue->front = queue->rear = 0;queue->maxSize = maxSize;return 1; // 初始化成功
}

3.销毁

          释放队列存储空间

// 销毁循环队列
void destroyCircularQueue(CircularQueue *queue) {free(queue->data);
}

4.清空

// 清空循环队列
void clearCircularQueue(CircularQueue *queue) {queue->front = queue->rear = 0;
}

5.空队列

        队头和队尾相同的时候为空队列。

// 判断循环队列是否为空
Status isEmptyCircularQueue(CircularQueue *queue) {return queue->front == queue->rear;
}

6.队列长度

        比较栈顶和栈顶的指针

// 获取循环队列长度
int circularQueueLength(CircularQueue *queue) {return (queue->rear - queue->front + queue->maxSize) % queue->maxSize;
}

7.获取队头

        获取队头元素。

// 获取循环队列的队首元素
Status getCircularQueueFront(CircularQueue *queue, ElemType *element) {if (isEmptyCircularQueue(queue)) {return 0; // 队列为空}*element = queue->data[queue->front];return 1; // 成功获取队首元素
}

8.入队

// 入队
Status enCircularQueue(CircularQueue *queue, ElemType element) {if ((queue->rear + 1) % queue->maxSize == queue->front) {return 0; // 队列已满}queue->data[queue->rear] = element;queue->rear = (queue->rear + 1) % queue->maxSize;return 1; // 入队成功
}

9.出队

// 出队
Status deCircularQueue(CircularQueue *queue, ElemType *element) {if (isEmptyCircularQueue(queue)) {return 0; // 队列为空}*element = queue->data[queue->front];queue->front = (queue->front + 1) % queue->maxSize;return 1; // 出队成功
}

 10.遍历队列

// 遍历循环队列
void traverseCircularQueue(CircularQueue *queue) {for (int i = queue->front; i != queue->rear; i = (i + 1) % queue->maxSize) {printf("%d ", queue->data[i]);}printf("\n");
}

11.完整代码

int main(int argc, const char *argv[]) {CircularQueue queue;int maxSize = 10; // 循环队列的最大容量initCircularQueue(&queue, maxSize); // 初始化循环队列// 测试入队for (int i = 1; i <= 5; ++i) {enCircularQueue(&queue, i * 10);}// 输出队列元素printf("队列元素:");traverseCircularQueue(&queue);// 获取队首元素ElemType frontElement;if (getCircularQueueFront(&queue, &frontElement)) {printf("队首元素:%d\n", frontElement);}// 测试出队ElemType element;for (int i = 0; i < 3; ++i) {if (deCircularQueue(&queue, &element)) {printf("出队元素:%d\n", element);}}// 输出队列元素printf("队列元素:");traverseCircularQueue(&queue);// 判断队列是否为空if (isEmptyCircularQueue(&queue)) {printf("队列为空\n");} else {printf("队列不为空\n");}// 获取队列长度printf("队列长度:%d\n", circularQueueLength(&queue));// 清空队列clearCircularQueue(&queue);// 判断队列是否为空if (isEmptyCircularQueue(&queue)) {printf("清空队列后,队列为空\n");} else {printf("清空队列后,队列不为空\n");}// 销毁队列destroyCircularQueue(&queue);return 0;
}

http://www.ppmy.cn/devtools/40157.html

相关文章

Oceanbase all-in-one单机版部署,通过MySQL客户端连接OB租户,DBEAVER 客户端连接MySQL租户。

一.Oceanbase all-in-one单机版部署 1.修改资源限制。 vim /etc/security/limits.conf root soft nofile 655350 root hard nofile 655350 * soft nofile 655350 * hard nofile 655350 * soft stack unlimited * hard stack unlimited * soft nproc 655360 * hard nproc 6553…

SpringCloud面试题

SpringCloud常见组件有哪些 注册中心组件&#xff1a;Eureka、Nacos 负载均衡组件&#xff1a;Ribbon 远程调用组件&#xff1a;OpenFeign 网关组件&#xff1a;Zuul、Gateway 服务保护组件&#xff1a;Hystrix、Sentinel 服务配置管理组件&#xff1a;SpringCloudConfig、Nac…

力扣题目汇总分析 利用树形DP解决问题

树里 任意两个节点之间的问题。而不是根节点到叶子节点的问题或者是父节点到子节点的问题。通通一个套路&#xff0c;即利用543的解题思路。 543.二叉树的直径 分析 明确&#xff1a;二叉树的 直径 是指树中任意两个节点之间最长路径的 长度。两个节点之间的最长路径是他们之…

PPMP_char3

PMPP char3 – Multidimensional grids and data ​ 五一过后&#xff0c;有些工作要赶&#xff0c;抽出时间更新一下。这一章基本都熟练掌握&#xff0c;在做习题过程中有一些思考。这里涉及到了一点点GEMM&#xff08;矩阵乘&#xff09;&#xff0c;GEMM有太多可深挖的了&a…

Postman工具介绍与安装

一、Postman介绍 Postman 乃是一款对 HTTP 协议予以支持的接口调试及测试工具&#xff0c;其突出特性在于功能强大&#xff0c;并且使用简便、易用性良好。不管是开发人员开展接口调试工作&#xff0c;还是测试人员进行接口测试任务&#xff0c;Postman 均属于首选工具之一。 接…

WebRTC实现多人通话-Mesh架构【保姆级源码教程】

一、Mesh架构 WebRTC&#xff08;Web Real-Time Communications&#xff09;中的Mesh架构是一种将多个终端之间两两进行连接&#xff0c;形成网状结构的通信模式。以下是关于WebRTC的Mesh架构的详细解释&#xff1a; 基本概念&#xff1a;在Mesh架构中&#xff0c;每个参与者…

webpack配置、插件使用案例

概念 本质上&#xff0c;webpack 是一个用于现代 JavaScript 应用程序的 静态模块打包工具。当 webpack 处理应用程序时&#xff0c;它会在内部从一个或多个入口点构建一个 依赖图(dependency graph)&#xff0c;然后将你项目中所需的每一个模块组合成一个或多个 bundles&…

【Unity Animation 2D】Unity Animation 2D骨骼绑定与动画制作

一、图片格式为png格式&#xff0c;并且角色各部分分离 图片参数设置 需要将Sprite Mode设置为Single&#xff0c;否则图片不能作为一个整体 1、创建骨骼 1.1 旋转Create Bone&#xff0c;点击鼠标左键确定骨骼位置&#xff0c;移动鼠标再次点击鼠标左键确定骨骼&#xff0c…