vlib队列 - C语言通用队列模块

vlib - queue队列

queue源代码仓库链接

基本概念

队列是一种先进先出(First In First Out,FIFO)的特殊数据结构,一般情况下它只有一个出口一个入口,从队尾进入从队头出,入队push,出队pop。
本文介绍的队列是C语言的通用队列,支持各种数据类型,采用连续地址的环形存储机制。

例子

int main()
{
	queue_t q = queue(int, 8); // 定义并构造一个最大容量为8的int型队列
	int i = 0;

	// 将 0,1,2,3,4 添加到队列中
	for (i = 0; i < 5; i++)
	{
		queue_push(q, &i);
	}
	i = 100; queue_insert(q, 2, &i, QUEUE_ORGIN_FRONT); // 在队头索引为2的位置插入100

	// 遍历队列的元素
	for (i = 0; i < queue_size(q); i++)
	{
		printf("q[%d] = %d\r\n", i, queue_at(q, int, i));
	}

	// 弹出队列的前两个元素
	if (queue_pop(q, &i)); printf("pop: %d\r\n", i);
	if (queue_pop(q, &i)); printf("pop: %d\r\n", i);

	// 使用完队列后进行删除
	_queue(q);

	system("pause");
	return 0;
}

结果:

q[0] = 0
q[1] = 1
q[2] = 100
q[3] = 2
q[4] = 3
q[5] = 4
pop: 0
pop: 1

特点

  • queue定义为queue_t类型
  • queue构造需确定类型和大小,支持各种类型和最大的容量
  • queue可以很方便的进行常用的入队出队操作
  • queue具备随机访问的操作,此方式可用直接获取或者修改队列中的元素
  • queue还提供插入、移除等操作方法,以及特殊的队列模式

常用方法

#define queue(type, capacity) // 构造
#define _queue(queue) // 删除
#define queue_at(queue, type, i) // 随机访问
int queue_size(queue_t queue); // 获取大小
int queue_push(queue_t queue, void* data); // 入队
int queue_pop(queue_t queue, void* data); // 出队
void queue_clear(queue_t queue); // 清空
int queue_insert(queue_t queue, int index, void* data, int orgin); // 插队
int queue_erase(queue_t queue, int begin, int end, int orgin); // 移除
int queue_alter_capacity(queue_t queue, int capacity); // 修改队列的最大容量
void queue_mode(queue_t queue, int mode, int set); // 设置或者取消某些特殊的队列模式
int queue_init(queue_t queue, void* array, int data_size, int capacity); // 根据已定义数组来初始化队列
void queue_deinit(queue_t queue); // 与初始化对应,去初始化

方法的使用具体看仓库介绍和源代码

最后

此模块为笔者本人编写,如果喜欢希望点赞给星支持,有漏洞或者修改建议欢迎留言交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include #include #include //队列最大长度 #define MAX_QUEUE 1024 //偷懒,就用静态队列了 static int mQueue[MAX_QUEUE]; //队列插入 void InsertData(int **Front, int **Rear) { if (*Rear + 1 == *Front && (*Rear + 1 - MAX_QUEUE != *Front)) { //当队列数据已满,返回 puts("Queue Size Overflow!\n"); return; } else if (*Rear - mQueue > MAX_QUEUE) { //实现的是类似循环队列,但由于是静态线性队列(数组) //而不是用链表来实现的,所以到静态队列(数组)尾部,尾指针自动指向(数组)头部 *Rear = mQueue; } puts("Input Data:"); scanf("%d", *Rear); //输入数据后,尾指针后移 *Rear += 1; } //从头指针删除一个队列中的数据 void DeleteData(int **Front, int **Rear) { if (*Front == *Rear) { //头指针尾指针重合,队列空,不能删除,返回 puts("Queue Empty!\n"); return; } else if (*Front - mQueue > MAX_QUEUE) { //参考 Rear *Front = mQueue; } //从头指针删除一个数据 *Front += 1; } //显示队列数据 void ShowData(int **Front, int **Rear) { int *temp; for (temp=*Front; temp!=*Rear; temp++) { printf("%d --> ", *temp); } puts("\n"); } void usage(void) { puts("1. Insert Data"); puts("2. Delete Data"); puts("3. Show Data"); } int main(int argc, char **argv) { //头指针,尾指针 //队列的一个特性 First in first out FIFO int *pFront, *pRear; int op_code; //初始化队列,头指针和尾指针此时指向的地址相同 pFront = pRear = mQueue; while (1) { usage(); scanf("%d", &op_code); switch (op_code) { case 1: printf("%p\n", pFront); printf("%d\n", *pFront); InsertData(&pFront, &pRear); break; case 2: DeleteData(&pFront, &pRear); break; case 3: ShowData(&pFront, &pRear); break; default: break; } } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值