数据结构——关于队列

1.队列的概念及结构


队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特性

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


2.队列的实现


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

这里是使用单链表实现,进行尾插头删


Queue.h 


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

typedef int QDataType;

typedef struct QueueNode
{
	struct QueueNode* next;//下一个
	QDataType val;//当前节点的值
}QNode;


// 队尾插入
//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
// 队头删除
//void QueuePop(QNode** pphead, QNode** pptail);

//为了避免上面那样定义多个参数和二级指针,所以创建下面的结构体
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);

//检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* pq);

 队尾插入
//void QueuePush(QNode** pphead, QNode** pptail, QDataType x);
 队头删除
//void QueuePop(QNode** pphead, QNode** pptail);

// 队尾插入
void QueuePush(QNode** pphead, QNode** pptail, QDataType x);

// 队头删除
void QueuePop(QNode** pphead, QNode** pptail);

如果出现上面那样需要定义多个参数和二级指针,可以创建下面的结构体

//如果出现需要定义多个参数和二级指针,可以创建下面的结构体
typedef struct Queue
{
	QNode* phead;//头指针
	QNode* ptail;//尾指针
	int size;
}Queue;


Queue.c

初始化

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

销毁

//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);
	//cur 当前节点
	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));
	//如果malloc失败
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->val = x;//因为现在要尾插的是x

	//如果ptail为NULL,说明一个节点都没有
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		//把新节点变成新的尾节点
		pq->ptail = newnode;
	}
	//如果不加size++那么实现QueueSize时就需要遍历
	pq->size++;
}

队头删除

// 队头删除
void QueuePop(Queue* pq)
{
	assert(pq);
	//判断有没有节点
	assert(pq->size != 0);

	//只有一个节点
	//phead的下一个节点为空
	if (pq->phead->next == NULL)
	{
		free(pq->phead);//释放头节点指向的“节点”

		//将头节点和尾节点只为NULL
		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);
	//head为空没有数据取
	assert(pq->phead);

	return pq->phead->val;
}

 取队尾的数据

//取队尾的数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	//ptail为空没有数据取
	assert(pq->ptail);

	return pq->ptail->val;
}

获取队列中有效元素个数

// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

检查是否为空

//检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}


3. 完整代码

#include"Queue.h"

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

//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);
	//cur 当前节点
	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));
	//如果malloc失败
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}

	newnode->next = NULL;
	newnode->val = x;//因为现在要尾插的是x

	//如果ptail为NULL,说明一个节点都没有
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		//把新节点变成新的尾节点
		pq->ptail = newnode;
	}
	//如果不加size++那么实现QueueSize时就需要遍历
	pq->size++;
}

// 队头删除
void QueuePop(Queue* pq)
{
	assert(pq);
	//判断有没有节点
	assert(pq->size != 0);

	//只有一个节点
	//phead的下一个节点为空
	if (pq->phead->next == NULL)
	{
		free(pq->phead);//释放头节点指向的“节点”

		//将头节点和尾节点只为NULL
		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);
	//head为空没有数据取
	assert(pq->phead);

	return pq->phead->val;
}

//取队尾的数据
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	//ptail为空没有数据取
	assert(pq->ptail);

	return pq->ptail->val;
}

// 获取队列中有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

//检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值