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;
}
相关推荐
啊阿狸不会拉杆3 小时前
《算法导论》第 21 章-用于不相交集合的数据结构
数据结构·c++·算法·随机森林
·白小白4 小时前
【数据结构】——顺序表链表(超详细解析!!!)
数据结构·链表
想不明白的过度思考者4 小时前
初识数据结构——优先级队列(堆!堆!堆!)
数据结构
啊阿狸不会拉杆5 小时前
《算法导论》第 18 章 - B 树
数据结构·c++·b树·算法·排序算法
再睡一夏就好6 小时前
【排序算法】⑦归并排序
c语言·数据结构·算法·排序算法·学习笔记
熬了夜的程序员18 小时前
【华为机试】208. 实现 Trie (前缀树)
数据结构·算法·华为od·华为
数据智能老司机1 天前
图算法趣味学——最短路径
数据结构·算法·云计算
gopher_looklook1 天前
Go并发实战:singleflight 源码解读与二次封装
数据结构·后端·go
终焉代码1 天前
【C++】STL二叉搜索树——map与set容器的基础结构
开发语言·数据结构·c++
无敌的大魔王1 天前
数据结构 双链表与LinkedList
数据结构