队列的实现

1. 队列

1.1 队列的概念及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头。
队列的实现方式有多种,包括基于数组的实现和基于链表的实现。基于数组的实现可能需要处理数组的扩容问题,而基于链表的实现则相对灵活,但在某些情况下可能会浪费一些内存空间。
这里采用的是链表的方式实现的。
在这里插入图片描述

1.2 队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
在这里插入图片描述

另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。
在这里插入图片描述

在这里插入图片描述

以下是队列的特点

  • 先进先出(FIFO):队列中的数据元素遵循先进先出的原则,即最先进入队列的元素将最先出队。
  • 限定插入和删除位置:队列只允许在表的前端(front)进行删除操作,称为出队;而在表的后端(rear)进行插入操作,称为入队。
  • 无元素时特性:当队列中没有元素时,称为空队列。在空队列中不能进行出队操作。
  • 队列操作的时间性能:在队列中,插入和删除操作在表尾和表头进行,不需要移动队列中的其他元素,因此时间复杂度为O(1)。

实现队列的基本操作

  • 入队(Enqueue):在队列的尾部插入一个元素。如果队列已满,则入队操作可能会失败。
  • 出队(Dequeue):从队列的头部移除一个元素,并返回该元素的值。如果队列为空,则出队操作可能会失败或返回一个特殊值(如空指针或异常)。
  • 查看队头(Front):返回队列头部的元素的值,但不移除该元素。如果队列为空,此操作可能会失败或返回一个特殊值。
  • 查看队尾(Rear):返回队列尾部的元素的值,但不移除该元素。如果队列为空,此操作可能会失败或返回一个特殊值。
  • 判断队列是否为空(IsEmpty):检查队列中是否包含任何元素。如果队列为空,则返回true;否则返回false。
  • 获取队列大小(Size):返回队列中元素的数量。
  • 清空队列(Clear):移除队列中的所有元素,使队列变为空状态。

2. 队列完整实现

2.1 链式结构:表示队列

用链表实现队列,因为更适合先进先出。

typedef int QDataType;

// 链式结构:表示队列
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType date;
}QNode;

2.2 队列的结构

结构体嵌套结构体指针,一个指向头,一个指向尾,一个记录长度。

// 队列的结构
typedef struct Queue
{
	QNode* phead;//头
	QNode* ptail;//尾
	int size;
}Queue;

2.3 初始化

不初始化就是随机值。

//初始化
void QueueInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

2.4 销毁

避免出现野指针的错误。

//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);

	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

2.5 入队

在队列的尾部插入一个元素。

//入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("QueuePush");
		return;
	}

	newnode->date = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		assert(pq->phead == NULL);

		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

2.6 出队

从队列的头部删除一个元素,并返回这个元素的值。

//出队
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	//1个节点
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	//多个节点
	else
	{
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}

	pq->size--;
}

2.7 获取队头数据

返回队列头部元素的值,但不删除它。

//获取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->date;
}

2.8 获取队尾数据

返回队列尾部元素的值

//获取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->date;
}

2.9 元素个数

返回队列中元素的数量。

//元素个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

2.10 判空

如果队列中没有任何元素,返回true;否则返回false。

//判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == NULL
		&& pq->ptail == NULL;
}

3. test主函数

#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"

void TestQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	printf("%d ", QueueFront(&q));
	QueuePush(&q, 6);
	QueuePush(&q, 7);
	QueuePush(&q, 8);

	printf("size: %d ", QueueSize(&q));
	printf("\n");

	while (!QueueEmpty(&q))
	{
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");

	QueueDestroy(&q);
}

int main()
{
	TestQueue();

	return 0;
}

4. Queue.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"


//初始化
void QueueInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

	//销毁
	void QueueDestroy(Queue* pq)
	{
		assert(pq);

		QNode* cur = pq->phead;
		while (cur)
		{
			QNode* next = cur->next;
			free(cur);
			cur = next;
		}

		pq->phead = NULL;
		pq->ptail = NULL;
		pq->size = 0;
	}

//入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("QueuePush");
		return;
	}

	newnode->date = x;
	newnode->next = NULL;

	if (pq->ptail == NULL)
	{
		assert(pq->phead == NULL);

		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}

	pq->size++;
}

//出队
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	//1个节点
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	//多个节点
	else
	{
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}

	pq->size--;
}

//获取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->date;
}

//获取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->date;
}

//元素个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

//判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == NULL
		&& pq->ptail == NULL;
}

5. Queue.h文件

#pragma once
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

typedef int QDataType;

// 链式结构:表示队列
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType date;
}QNode;

// 队列的结构
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

//初始化
void QueueInit(Queue* pq);

//销毁
void QueueDestroy(Queue* pq);

//入队
void QueuePush(Queue* pq, QDataType x);

//出队
void QueuePop(Queue* pq);

//获取队头数据
QDataType QueueFront(Queue* pq);

//获取队尾数据
QDataType QueueBack(Queue* pq);

//元素个数
int QueueSize(Queue* pq);

//判空
bool QueueEmpty(Queue* pq);

6. 测试

在这里插入图片描述

  • 59
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蹋雾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值