队列的实现

1.队列的基本概念

队列是一种线性的数据结构,它有两个端点,分别为队头(Front)和队尾(Rear)。元素从队尾进入队列(这个操作称为入队,Enqueue),然后从队头离开队列(此操作叫做出队,Dequeue)。例如,在一个银行排队办理业务的场景中,顾客们依次在队伍末尾排队(入队),而柜员按顺序从队伍开头叫号为顾客办理业务(出队),体现的就是队列先进先出的特性。

除了入队和出队这两个核心操作外,队列通常还需要具备一些辅助操作,比如查看队头元素(Peek)、判断队列是否为空(IsEmpty)以及获取队列中元素的个数(Size)等,这些操作能帮助我们更好地管理和了解队列的状态。

 2.队列的实现

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

2.1队列节点结构

//重命名队列元素类型
typedef int QDataType;

//队列节点结构
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QueueNode;

2.2队列结构


typedef struct Queue
{
	QueueNode* phead;
	QueueNode* ptail;
	int size;//队列有效元素个数
}Queue;

2.3队列的初始化


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

2.4队尾入队列

void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	//申请新节点
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->phead == NULL)//队列为空
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
	pq->size++;
}

2.5队列判空

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead ==NULL && pq->ptail==NULL;
}

2.6对头出队列

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->ptail == pq->phead)
	{
		free(pq->phead);
		pq->phead =pq->ptail = NULL;
	}
	else
	{
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	--pq->size;
}

2.7取队头数据

QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}

2.8取队尾数据

QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}

2.9队列有效元素个数

int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}

2.10销毁队列

void QueueDestroy(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

3.队列完整代码

queue.h

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

//重命名队列元素类型
typedef int QDataType;

//队列节点结构
typedef struct QueueNode
{
	QDataType data;
	struct QueueNode* next;
}QueueNode;

//队列结构
typedef struct Queue
{
	QueueNode* phead;
	QueueNode* ptail;
	int size;//队列有效元素个数
}Queue;

//初始化队列
void QueueInit(Queue* pq);

//队尾入队列
void QueuePush(Queue* pq, QDataType x);
//队头出队列
void QueuePop(Queue* pq);
//队列判空
bool QueueEmpty(Queue* pq);
//取队头数据
QDataType QueueFront(Queue* pq);
// 取队尾数据
QDataType QueueBack(Queue* pq);
// 队列有效元素个数
int QueueSize(Queue* pq);
//销毁队列
void QueueDestroy(Queue* pq);

queue.c

#include "queue.h"

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

//队尾入队列
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	//申请新节点
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->phead == NULL)//队列为空
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
	pq->size++;
}
//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead ==NULL && pq->ptail==NULL;
}
//队头出队列
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->ptail == pq->phead)
	{
		free(pq->phead);
		pq->phead =pq->ptail = NULL;
	}
	else
	{
		QueueNode* 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);
	return pq->size;
}
//销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

text.c

#include "queue.h"

void text()
{
	Queue p;
	QueueInit(&p);
	QueuePush(&p, 2);
	QueuePush(&p, 3);
	QueuePush(&p, 4);
	QueuePop(&p);
	int a = QueueFront(&p);
	int b = QueueBack(&p);
	int c = QueueSize(&p);
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n",c);
	QueueDestroy(&p);
}
int main()
{
	text();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学无止境\n

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

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

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

打赏作者

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

抵扣说明:

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

余额充值