【数据结构】队列基本操作的实现(C语言)


🚀 作者简介:一名在后端领域学习,并渴望能够学有所成的追梦人。
🐌 个人主页:蜗牛牛啊
🔥 系列专栏:🛹数据结构、🛴C++
📕 学习格言:博观而约取,厚积而薄发
🌹 欢迎进来的小伙伴,如果小伙伴们在学习的过程中,发现有需要纠正的地方,烦请指正,希望能够与诸君一同成长! 🌹


一、概念和结构

队列只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 。
入队列:进行插入操作的一端称为队尾 。
出队列:进行删除操作的一端称为队头。

队列

二、基本操作的实现

定义结构体:

typedef int QDataType;
//队列中结点
typedef struct QueueNode
{
	QDataType val;//结点值
	struct QueueNode* next;//指向下一个结点的指针
}QNode;
typedef struct Queue {
	QNode* head;//队列头结点的指针
	QNode* tail;//队列尾结点的指针
	int size;//队列中元素个数
}Queue;

1.初始化

void QueueInit(Queue* pq)//初始化
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

2.判断是否为空

bool QueueEmpty(Queue* pq)//判断是否为空
{
	assert(pq);
	return pq->head == NULL && pq->tail == NULL; //当队列头节点的指针和尾结点的指针都为空时队列为空
}

3.入队

void QueuePush(Queue* pq, QDataType x)//入队
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode)); //创建节点
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
    //将节点赋值
	newnode->val = x;
	newnode->next = NULL;
    //当队列为空时
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
    //当队列不为空时
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

入队时判断当队列为空时pq->head = pq->tail = NULL,不能对pq->tail解引用。

4.出队

void QueuePop(Queue* pq)//出队列
{
	assert(pq);
	assert(!QueueEmpty(pq)); //判断队列是否为空
    //当队列中只剩一个结点时
	if (pq->head->next == NULL)
	{
		free(pq->head);//释放节点
		pq->head = pq->tail = NULL;//队列置空
	}
    //队列中有多个结点时
	else
	{
		QNode* cur = pq->head;//记录下一个节点
		pq->head = cur->next;
		free(cur);//释放头节点
	}
	pq->size--;//有效个数--
}

出队时要判断队列是否为空,assert(!QueueEmpty(pq))

5.打印

void QueuePrint(Queue* pq)//打印
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}

注意循环条件while (cur),不能写为while(cur!=pq->tail)

6.返回队头的值

QDataType QueueFront(Queue* pq)//返回队头的值
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->val;
}

7.返回队尾的值

QDataType QueueBack(Queue* pq)//返回队尾的值
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->val;
}

8.统计队列中有效值个数

int QueueSize(Queue* pq)//返回队列中有效值个数
{
	assert(pq);
	//int size = 0;
	//QNode* cur = pq->head;
	//while (cur)
	//{
	//	size++;
	//	cur = cur->next;
	//}
	//return size;
	return pq->size;
}

9.销毁

void QueueDestroy(Queue* pq)//销毁
{
	assert(pq);
	QNode* cur = pq->head;
    //遍历,一个一个的释放空间
	while (cur)
	{
		QNode* del = cur->next;
		free(cur);
		cur = del;
	}
	pq->head = pq->tail = NULL;//置空
}

三、测试代码

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
typedef int QDataType;
typedef struct QueueNode
{
	QDataType val;
	struct QueueNode* next;
}QNode;
typedef struct Queue {
	QNode* head;
	QNode* tail;
	int size;
}Queue;
void QueueInit(Queue* pq)//初始化
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)//销毁
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* del = cur->next;
		free(cur);
		cur = del;
	}
	pq->head = pq->tail = NULL;
}
void QueuePrint(Queue* pq)//打印
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->val);
		cur = cur->next;
	}
	printf("\n");
}
bool QueueEmpty(Queue* pq)//判断是否为空
{
	assert(pq);
	return pq->head == NULL && pq->tail == NULL;
}
void QueuePush(Queue* pq, QDataType x)//入队
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}
void QueuePop(Queue* pq)//出队列
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* cur = pq->head;
		pq->head = cur->next;
		free(cur);
	}
	pq->size--;
}
QDataType QueueFront(Queue* pq)//返回队头的值
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->head->val;
}
QDataType QueueBack(Queue* pq)//返回队尾的值
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->val;
}

int QueueSize(Queue* pq)//返回队列中有效值个数
{
	assert(pq);
	//int size = 0;
	//QNode* cur = pq->head;
	//while (cur)
	//{
	//	size++;
	//	cur = cur->next;
	//}
	//return size;
	return pq->size;
}
void TeseQueue()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);
	QueuePush(&q, 5);
	QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);
	QueuePop(&q);

	QueuePrint(&q);
	printf("%d\n", QueueFront(&q));
	printf("%d\n", QueueBack(&q));
	if(!QueueEmpty(&q))
		printf("%d\n", QueueSize(&q));

	QueueDestroy(&q);
}
int main()
{
	TeseQueue();
	return 0;
}
  • 45
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 35
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不 良

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

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

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

打赏作者

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

抵扣说明:

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

余额充值