当前位置:   article > 正文

队列的实现_队列实现方法

队列实现方法

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.2 队列的结构

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

// 队列的结构
typedef struct Queue
{
	QNode* phead;//头
	QNode* ptail;//尾
	int size;
}Queue;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.3 初始化

不初始化就是随机值。

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

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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++;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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--;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.7 获取队头数据

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

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

	return pq->phead->date;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.8 获取队尾数据

返回队列尾部元素的值

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

	return pq->ptail->date;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.9 元素个数

返回队列中元素的数量。

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

	return pq->size;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.10 判空

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

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

	return pq->phead == NULL
		&& pq->ptail == NULL;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

6. 测试

在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号