【数据结构——队列的实现(单链表)】

数据结构------队列的实现(单链表)

一.队列

1.1队列的概念及结构

二.队列的实现

2.1 头文件的实现------(Queue.h)

c 复制代码
Queue.h
c 复制代码
#pragma once


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

typedef int QDataType;
typedef struct QueueNode
{
	QDataType val;
	struct QueueNode* next;
}QNode;


typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

//初始化/销毁
void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);

//出队/入队
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
//获取队头元素/队尾元素
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
//判空/统计队列元素个数
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);

2.2 源文件的实现------ (Queue.c)

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


//初始化/销毁
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* Next = cur->next;
		free(cur);
		cur = Next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

//队尾入队/队首出队
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* Newnode = (QNode*)malloc(sizeof(QNode));
	if (Newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	Newnode->val = 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++;
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->phead);
	QNode* del = pq->phead;
	pq->phead = pq->phead->next;
	free(del);
	del = NULL;

	if (pq->phead == NULL)
		pq->ptail = NULL;
	pq->size--;


}

//获取队头元素/队尾元素
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);

	return pq->phead->val;
}
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);

	return pq->ptail->val;

}
//判空/统计队列元素个数
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

2.3 源文件的实现------ (test.c)

c 复制代码
test.c
c 复制代码
#include"Queue.h"


int main()
{
	Queue S;
	QueueInit(&S);
	QueuePush(&S, 1);
	QueuePush(&S, 2);
	printf("%d ", QueueFront(&S));
	QueuePop(&S);
	QueuePush(&S, 3);
	QueuePush(&S, 4);
	printf("%d ", QueueFront(&S));
	QueuePop(&S);
	QueuePush(&S, 5);

	while (!QueueEmpty(&S))
	{
		printf("%d ", QueueFront(&S));
		QueuePop(&S);
	}
	QueueDestroy(&S);
}

三.队列的实际数据测试展示

1.出入队列的方式:队尾进入,对首出队列。
2.出队列和入队列的关系是:一对一的。

3.1正常出队列入队列

3.2 入队列的同时存在出队列

相关推荐
佩佩(@ 。 @)18 分钟前
嵌入式:走馬燈-stm32GPIOF_LED9、10; GPIOE_D10、D12 流水綫蜂鸣器
stm32·嵌入式硬件·算法
知彼解己23 分钟前
字符串大数相加:从初稿到优化的思路演进
java·开发语言·算法
haing20191 小时前
使用deboor法计算三次B样条曲线在参数为u处的位置的方法介绍
算法·b样条曲线·deboor
qq_352109521 小时前
旋转数字矩阵 od
算法
大阳1231 小时前
51单片机4(温度传感器DS18B20)
开发语言·单片机·嵌入式硬件·算法·51单片机
iナナ1 小时前
Java优选算法——二分查找
数据结构·算法·leetcode
l1t2 小时前
利用美团龙猫添加xlsx的sheet.xml读取sharedStrings.xml中共享字符串输出到csv功能
xml·c语言·数据结构·人工智能·算法·解析器
宇钶宇夕2 小时前
西门子 S7-200 SMART PLC 编程:转换 / 定时器 / 计数器指令详解 + 实战案例(指令讲解篇)
运维·算法·自动化
我叫汪枫2 小时前
Spring Boot图片验证码功能实现详解 - 从零开始到完美运行
java·前端·javascript·css·算法·html
l1t2 小时前
how to build tbox xml into the demo
xml·linux·c语言·parser·tbox