数据结构之队列

目录

一、队列的概念

二、队列的实现

[2.1 队列的初始化](#2.1 队列的初始化)

[2.2 入列](#2.2 入列)

[2.3 出列](#2.3 出列)

[2.4 获取队头元素](#2.4 获取队头元素)

[2.5 获取队尾元素](#2.5 获取队尾元素)

[2.6 获取队列内有效元素个数](#2.6 获取队列内有效元素个数)

[2.7 销毁队列](#2.7 销毁队列)

三、测试代码


一、队列的概念

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头。
下面用一张图来看清楚队列的结构:

二、队列的实现

与栈的实现不同,队列的实现一般是基于数组的。虽然队列既可以数组也可以用链表的结构实现,但是在实际使用中,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

2.1 队列的初始化

cpp 复制代码
void QueueInit(Queue* q)
{
	q->front = NULL;
	q->rear = NULL;
}

2.2 入列

cpp 复制代码
QNode* buyNewnode(QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
	QNode* newnode = buyNewnode(data);
	assert(q);
	if (q->rear == NULL)
	{
		q->front = newnode;
		q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
}

2.3 出列

cpp 复制代码
// 队头出队列
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->front != NULL);
	QNode* Next = q->front->next;
	if (q->front == NULL)
	{
		q->rear = NULL;
	}
	free(q->front);
	q->front = Next;
}

2.4 获取队头元素

cpp 复制代码
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front != NULL);
	return q->front->data;
}

2.5 获取队尾元素

cpp 复制代码
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->rear != NULL);
	return q->rear->data;
}

2.6 获取队列内有效元素个数

cpp 复制代码
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
	assert(q);
	int size = 0;
	QNode* current = q->front;
	while (current != NULL)
	{
		size++;
		current = current->next;
	}
	return size;
}

2.7 销毁队列

cpp 复制代码
// 销毁队列
void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* current = q->front;
	while (current != NULL)
	{
		QNode* temp = current;
		current = current->next;
		free(temp);
	}
	q->front = NULL;
	q->rear = NULL;
}

三、测试代码

cpp 复制代码
// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNode;
// 队列的结构
typedef struct Queue
{
	QNode* front;
	QNode* rear;
}Queue;
// 初始化队列
void QueueInit(Queue* q)
{
	q->front = NULL;
	q->rear = NULL;
}
QNode* buyNewnode(QDataType x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
	QNode* newnode = buyNewnode(data);
	assert(q);
	if (q->rear == NULL)
	{
		q->front = newnode;
		q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
}
// 队头出队列
void QueuePop(Queue* q)
{
	assert(q);
	assert(q->front != NULL);
	QNode* Next = q->front->next;
	if (q->front == NULL)
	{
		q->rear = NULL;
	}
	free(q->front);
	q->front = Next;
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->front != NULL);
	return q->front->data;
}
// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->rear != NULL);
	return q->rear->data;
}
// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
	assert(q);
	int size = 0;
	QNode* current = q->front;
	while (current != NULL)
	{
		size++;
		current = current->next;
	}
	return size;
}
// 销毁队列
void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* current = q->front;
	while (current != NULL)
	{
		QNode* temp = current;
		current = current->next;
		free(temp);
	}
	q->front = NULL;
	q->rear = NULL;
}
int main()
{
	Queue q;
	QueueInit(&q);

	// 测试入队
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

	// 测试队列头部元素
	printf("队列头部元素: %d\n", QueueFront(&q)); // 应该输出1

	// 测试队列尾部元素
	printf("队列尾部元素: %d\n", QueueBack(&q)); // 应该输出4

	// 测试队列大小
	printf("队列大小: %d\n", QueueSize(&q)); // 应该输出4

	// 测试出队
	QueuePop(&q);
	printf("队列头部元素: %d\n", QueueFront(&q)); // 应该输出2

	// 测试队列大小
	printf("队列大小: %d\n", QueueSize(&q)); // 应该输出3

	// 清空队列
	while (QueueSize(&q) > 0)
	{
		printf("弹出元素: %d\n", QueueFront(&q));
		QueuePop(&q);
	}

	// 测试队列是否为空
	printf("队列是否为空: %d\n", QueueSize(&q) == 0); // 应该输出1(true)

	// 销毁队列
	QueueDestroy(&q);

	return 0;
}
相关推荐
superlls1 天前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
田里的水稻1 天前
C++_队列编码实例,从末端添加对象,同时把头部的对象剔除掉,中的队列长度为设置长度NUM_OBJ
java·c++·算法
纪元A梦1 天前
贪心算法应用:保险理赔调度问题详解
算法·贪心算法
Ripple123121 天前
数据结构:顺序表与链表
数据结构·链表
Jayden_Ruan1 天前
C++逆向输出一个字符串(三)
开发语言·c++·算法
一个响当当的名号1 天前
B树,B+树,B*树(无代码)
数据结构·b树
点云SLAM1 天前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
叙白冲冲1 天前
哈希算法以及面试答法
算法·面试·哈希算法
YuTaoShao1 天前
【LeetCode 每日一题】1277. 统计全为 1 的正方形子矩阵
算法·leetcode·矩阵
古译汉书1 天前
嵌入式铁头山羊stm32-ADC实现定时器触发的注入序列的单通道转换-Day26
开发语言·数据结构·stm32·单片机·嵌入式硬件·算法