数据结构6—队列(附源码)

1.队列

1.1 概念与结构

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

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

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

2.源码

2.1 Queue.h

cpp 复制代码
#pragma once

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

//定义队列结构

typedef int QDataType;

typedef struct QueueNode
{
	int 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);//队列的销毁

2.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;
	//ptail  newnode
	if (pq->phead == NULL)//队列为空
	{
		pq->phead = pq->ptail = newnode;
	}
	else//队列不为空
	{
		//队列不为空
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
	pq->size++;
}
//
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--;
}

//空位true ,非空为false
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead = NULL && pq->ptail == NULL;
}
//
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;
}

2.3 test.c

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

void QueueTest01()
{
	Queue q;
	QueueInit(&q);
	QueuePush(&q, 1);
	QueuePush(&q, 2);
	QueuePush(&q, 3);

	//QueuePop(&q);
	printf("head:&d\n", QueueFront(&q));
	printf("head:&d\n", QueueBack(&q));

}


int main()
{
	QueueTest01();

	return 0;
}
相关推荐
苏纪云10 分钟前
蓝桥杯考前突击
c++·算法·蓝桥杯
W230357657314 分钟前
经典算法详解:最长公共子序列 (LCS) —— 从暴力递归到动态规划完整实现
算法·动态规划·最长子序列
雷工笔记16 分钟前
MES / WMS / AGV 交互时序图及生产管理模块界面设计清单
人工智能·笔记
pzx_00122 分钟前
【优化器】 随机梯度下降 SGD 详解
人工智能·python·算法
大邳草民23 分钟前
Python 中 global 与 nonlocal 的语义与机制
开发语言·笔记·python
小肝一下32 分钟前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
landuochong20040 分钟前
claude-obsidian 再升级
人工智能·笔记·claudecode
CheerWWW1 小时前
C++学习笔记——线程、计时器、多维数组、排序
c++·笔记·学习
无限进步_1 小时前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio
浅时光_c1 小时前
12 指针
c语言·开发语言