C语言 数据结构 【队列】动态模拟实现

引言

用动态方式模拟实现队列的各个接口

一、队列的结构与概念

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

入队列:进行插入操作的一端称为队尾

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

二、队的模拟实现

队列底层结构选型

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

分三个文件来写:

cpp 复制代码
test.c   //测试队列的各个接口是否正确
Queue.h  //实现队列需要的头文件
Queue.c  //队列各个接口的实现

1、定义队列的结构

在Queue.h中定义

cpp 复制代码
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int QDataType;

typedef struct QueueNode  //定义队列的结点
{
	QDataType data;          //结点里面的内容
	struct QueueNode* next;  //下一个结点的位置
}QueueNode;

typedef struct Queue   //定义队列的结构
{  
	QueueNode* phead;  //指向队尾的指针
	QueueNode* ptail;  //指向队头的指针
	// int size;  也可以加一个记录链表元素个数的变量
}Queue;

2、初始化、入队

cpp 复制代码
//初始化
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++
}

3、队列判空、出队列

cpp 复制代码
//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}

//出队------队头
void QueuePop(Queue* pq)
{
	assert(!QueueEmpty(pq));
	//只有一个结点,phead和ptail都设置为空
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QueueNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	//pq->size--;
}

4、取队头元素、取队尾元素

cpp 复制代码
//取队头元素
QDataType QueueFront(Queue* pq)
{
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}

//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}

5、队列中元素个数、销毁队列

cpp 复制代码
//队列有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	//法一:遍历链表--适用于不会频繁调用队列有效数据个数的场景
	QueueNode* pcur = pq->phead;
	int size = 0;
	while (pcur)
	{
		size++;
		pcur = pcur->next;
	}
	return size;
	//法二:遍历链表--适用于频繁调用队列有效数据个数的场景
	//return pq->size;  即在队列里面创建一个size变量用来记录个数,
}
//销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	//pq->size = 0
}

三、所以测试代码

1.在Queue.h中的代码

cpp 复制代码
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.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 QueueDestroy(Queue* pq);

//入队------队尾
void QueuePush(Queue* pq, QDataType x);

//出队------队头
void QueuePop(Queue* pq);
//队列判空
bool QueueEmpty(Queue* pq);
//队列有效元素个数
int QueueSize(Queue* pq);

//取队头数据
QDataType QueueFront(Queue* pq);
//取队尾数据
QDataType QueueBack(Queue* pq);

2.在Queue.c中的代码

cpp 复制代码
#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;
}

//出队------队头
void QueuePop(Queue* pq)
{
	assert(!QueueEmpty(pq));
	//只有一个结点,phead和ptail都设置为空
	if (pq->phead == pq->ptail)
	{
		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(!QueueEmpty(pq));
	return pq->phead->data;
}

//取队尾数据
QDataType QueueBack(Queue* pq)
{
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}

//队列有效元素个数
int QueueSize(Queue* pq)
{
	assert(pq);
	//法一:遍历链表--适用于不会频繁调用队列有效数据个数的场景
	QueueNode* pcur = pq->phead;
	int size = 0;
	while (pcur)
	{
		size++;
		pcur = pcur->next;
	}
	return size;
	//法二:遍历链表--适用于频繁调用队列有效数据个数的场景
	//return pq->size;  即在队列里面创建一个size变量用来记录个数,
}
//销毁队列
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
	//pq->size = 0
}

3.在test.c中的代码

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

int main()
{
	Queue qu;
	QueueInit(&qu);
	QueuePush(&qu, 1);
	QueuePush(&qu, 2);
 	QueuePush(&qu, 3);
	QueuePop(&qu);
	int front = QueueFront(&qu);
	int rear = QueueBack(&qu);
	printf("%d\n", front);
	printf("%d\n", rear);
	printf("%d\n", QueueSize(&qu));
	QueueDestroy(&qu);

	return 0;
}
相关推荐
Hanson Huang4 小时前
【数据结构】堆排序详细图解
java·数据结构·排序算法·堆排序
Susea&4 小时前
数据结构初阶:队列
c语言·开发语言·数据结构
序属秋秋秋4 小时前
算法基础_数据结构【单链表 + 双链表 + 栈 + 队列 + 单调栈 + 单调队列】
c语言·数据结构·c++·算法
purrrew6 小时前
【数据结构_5】链表(模拟实现以及leetcode上链表相关的题目)
数据结构·leetcode·链表
挺6的还7 小时前
4.B-树
数据结构·b树
Tanecious.7 小时前
初阶数据结构--二叉树OJ训练
数据结构
x_feng_x7 小时前
数据结构与算法 - 数据结构与算法进阶
数据结构·python·算法
1024熙8 小时前
【C++】——lambda表达式
开发语言·数据结构·c++·算法·lambda表达式
the sun348 小时前
数据结构---跳表
数据结构
小黑屋的黑小子9 小时前
【数据结构】反射、枚举以及lambda表达式
数据结构·面试·枚举·lambda表达式·反射机制