队列算法【基于顺序表的环形队列】

2024-08-30 01:04
文章标签 算法 队列 顺序 环形

本文主要是介绍队列算法【基于顺序表的环形队列】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*【环形缓冲区(顺序表版本)】*/
typedef struct
{int* data;int size;
}g_tVector;
/* 顺序表的初始化 */
void VectorInit(g_tVector* vector, int len)
{if (NULL == vector)  return;vector->data = (int*)malloc(sizeof(int) * len);vector->size = len;return;
}
/* 顺序表的清理 */
void VectorClear(g_tVector* vector)
{if (NULL == vector)  return;free(vector->data);return;
}
/* 顺序表的写入数据 */
bool VectorWrite(g_tVector* vector, int index , int data)
{if (NULL == vector)  return false;vector->data[index] = data;return true;
}
/* 顺序表的读出数据 */
bool VectorRead(g_tVector* vector, int index, int** data)
{if (NULL == vector)  return false;**data = vector->data[index];return true;
}/************************************************/
typedef struct
{g_tVector* vector;int count, head, tail, len;void (*vectorInit)(g_tVector*, int);void (*vectorClear)(g_tVector*);bool (*vectorWrite)(g_tVector*, int, int);bool (*vectorRead)(g_tVector*, int, int**);
}g_tQueue;/* 环形缓冲区的初始化 */
void QueueInit(g_tQueue* queue, int len)
{if (NULL == queue) return;queue->vector = (g_tVector*)malloc(sizeof(g_tVector));queue->vectorInit = VectorInit;queue->vectorClear = VectorClear;queue->vectorRead = VectorRead;queue->vectorWrite = VectorWrite;queue->vectorInit(queue->vector, len);queue->len = len;queue->head = queue->tail = queue->count = 0;printf("init:%d, %d, %d, %d\r\n", queue->len, queue->count, queue->head, queue->tail);return;
}/* 环形缓冲区申请内存的释放 */
void QueueClear(g_tQueue* queue)
{if (NULL == queue) return;VectorClear(queue->vector);free(queue->vector);free(queue);return;
}bool QueueIsEmpty(g_tQueue* queue)
{if (NULL == queue) return false;return (queue->count == 0);
}bool QueueIsFull(g_tQueue* queue)
{if (NULL == queue) return false;//printf("data:%d, %d\r\n", queue->count, queue->len);return (queue->count == queue->len);
}/* 从环形缓冲区取出数据 */
bool PopQueue(g_tQueue* queue, int* data)
{if ((NULL == queue)|| (NULL == data) || QueueIsEmpty(queue)) return false;queue->vectorRead(queue->vector, queue->head, &data);if (queue->head < queue->len-1)queue->head++;      //头部索引向后移动elsequeue->head = 0;    //头部指向尾部queue->count--;   //环形缓冲区存储的数据量return true;
}
/* 向环形缓冲区压入数据 */
bool PushQueue(g_tQueue* queue, int data)
{if ((NULL == queue) || QueueIsFull(queue)) return false;queue->vectorWrite(queue->vector, queue->tail, data);if (queue->tail < queue->len-1)queue->tail++;elsequeue->tail = 0;queue->count++;return true;
}int main()
{g_tQueue queue;bool ret;int data;QueueInit(&queue, 5);for (int i = 0; i < 6; i++){data = rand() % 100;ret = PushQueue(&queue, data);if (!ret)printf("%d push is error\r\n", i);elseprintf("push %d\r\n", data);}ret = PopQueue(&queue, &data);if (ret)printf("pop data is %d\r\n", data);return 0;
}

运行结果如下所示:

这篇关于队列算法【基于顺序表的环形队列】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119356

相关文章

Java中常见队列举例详解(非线程安全)

《Java中常见队列举例详解(非线程安全)》队列用于模拟队列这种数据结构,队列通常是指先进先出的容器,:本文主要介绍Java中常见队列(非线程安全)的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一.队列定义 二.常见接口 三.常见实现类3.1 ArrayDeque3.1.1 实现原理3.1.2

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

MySQL中SQL的执行顺序详解

《MySQL中SQL的执行顺序详解》:本文主要介绍MySQL中SQL的执行顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql中SQL的执行顺序SQL执行顺序MySQL的执行顺序SELECT语句定义SELECT语句执行顺序总结MySQL中SQL的执行顺序

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结