【数据结构】队列---C语言版(详解!!!)

文章目录

🐸一、队列的概念及结构

🍄1、队列的概念定义

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

  • 入队列:进行插入操作的一端称为队尾
  • 出队列:进行删除操作的一端称为队头

🍄2、动图演示

🌰可以想象成排队去食堂打饭,前面先打完饭的就从队头先走了,后来的就需要在后面队尾继续排队

🐸二、队列的实现

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

🐸三、链表结构队列详解

🍎创建队列的结构

🥰这里先创建三个文件:

1️⃣:Queue.h文件用于函数的声明

2️⃣:Queue.c文件用于函数的定义

3️⃣:Test.c文件用于测试函数
建立三个文件的目的: 将队列作为一个项目来进行编写,方便我们的学习与观察。

⭕接口1:定义结构体(QNode、Queue)

🚩这里需要定义两个结构体:QNode、Queue,分别表示:队列链表每个节点结构和整个队列链表结构

🥰请看代码与注释👇

c 复制代码
//自定义类型
typedef int QDataType;

//队列链表每个节点结构
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

//整个队列链表结构
typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

⭕接口2:初始化(QueueInit)

🥰请看代码与注释👇

c 复制代码
//初始化
void QueueInit(Queue* pq)
{
	//断言传入指针不为NULL
	assert(pq);

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

⭕接口3:销毁(QueueDestroy)

🥰请看代码与注释👇

c 复制代码
//销毁
void QueueDestroy(Queue* pq)
{
	//断言传入指针不为NULL
	assert(pq);

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

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

⭕接口4:入队列(QueuePush)

🥰请看代码与注释👇

c 复制代码
//入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

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

	newnode->data = 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++;
}

⭕接口5:出队列(QueuePop)

🥰请看代码与注释👇

c 复制代码
//出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

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

	pq->size--;
}

⭕接口6:取队头数据(QueueFront)

🥰请看代码与注释👇

c 复制代码
//获取队头数据
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->phead->data;
}

⭕接口7:取队尾数据(QueueBack)

🥰请看代码与注释👇

c 复制代码
//获取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->ptail->data;
}

⭕接口8:获取队列大小(QueueSize)

🥰请看代码与注释👇

c 复制代码
//获取队列大小
int QueueSize(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->size;
}

⭕接口9:判空(QueueEmpty)

🥰请看代码与注释👇

c 复制代码
//判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	//return pq->phead == NULL && pq->ptail == NULL;
	return pq->size == 0;
}

🐸四、完整代码

🥝Queue.h

c 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int QDataType;

//队列链表每个节点
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}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);

🥝Queue.c

c 复制代码
#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 = pq->ptail = NULL;
	pq->size = 0;
}

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

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

	newnode->data = 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;
	}
	//2、多个节点
	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->data;
}

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

	return pq->ptail->data;
}

//获取队列大小
int QueueSize(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->size;
}

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

	//return pq->phead == NULL && pq->ptail == NULL;
	return pq->size == 0;
}

🥝Test.c

c 复制代码
#include"Queue.h"

//入队列测试
void TestQueue1()
{
	Queue q;
	QueueInit(&q);

	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);
	QueuePush(&q, 4);

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

	QueueDestroy(&q);
}

//测试
void TestQueue2()
{
	Queue q;
	QueueInit(&q);

	QueuePush(&q, 1);
	QueuePush(&q, 2);

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

	QueueDestroy(&q);
}


int main()
{
	//TestQueue1();
	//TestQueue2();

	return 0;
}

🥰这期内容相对比较简单,希望烙铁们可以理解消化哦!

总结🥰
以上就是 【数据结构】队列---C语言版 的全部内容啦🥳🥳🥳🥳
本文章所在【数据结构与算法】专栏,感兴趣的烙铁可以订阅本专栏哦🥳🥳🥳
前途很远,也很暗,但是不要怕,不怕的人面前才有路。💕💕💕
小的会继续学习,继续努力带来更好的作品😊😊😊
创作写文不易,还多请各位大佬uu们多多支持哦🥰🥰🥰

相关推荐
蒋星熠2 分钟前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
小莞尔8 分钟前
【51单片机】【protues仿真】基于51单片机宠物投食系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小欣加油28 分钟前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream32 分钟前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
王璐WL39 分钟前
【c++】c++第一课:命名空间
数据结构·c++·算法
空白到白1 小时前
机器学习-聚类
人工智能·算法·机器学习·聚类
小马学嵌入式~1 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
索迪迈科技1 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
zzzsde2 小时前
【数据结构】队列
数据结构·算法
芒克芒克2 小时前
LeetCode 面试经典 150 题:删除有序数组中的重复项(双指针思想解法详解)
算法