引言
用动态方式模拟实现队列的各个接口
一、队列的结构与概念
概念:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出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;
}