C语言/数据结构——队列的实现

一.前言

今天我们来聊一聊数据结构的一部分------队列。今天我们主要涉及队列的基本概念结构,以及队列的基本实现。

二.正文

1.1队列

1.12队列的概念及结构

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

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

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

1.13队列的实现

队列也可以用数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。 下面是我学习过程中老师的课件图表:

我们使用单链表实现队列的过程,就像我们之前所分享------单链表实现数据的增删查改

差不多。有兴趣的小伙伴可以了解一下:https://blog.csdn.net/yiqingaa/article/details/138206746?spm=1001.2014.3001.5501

不过和单链表实现数据的增删查改不同的是,这里我们又多定义了一个结构体,用来存放链表的头/尾指针,以及节点个数。

这样设计的好处是:我们不需要使用二级指针,而且也不用传太多的参数。

cpp 复制代码
struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
};

其他的内容就没有过多改动。

Queue.h
cpp 复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QDataType;
struct QueueNode
{
	struct QueueNode* next;
	QDataType val;
};
typedef struct QueueNode QNode;
struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
};
typedef struct Queue Queue;
void QueueInit(Queue* ps);//队列的初始化
void QueueDestroy(Queue* ps);//队列的销毁

void QueuePush(Queue* ps,QDataType x);//队尾插入数据
void QueuePop(Queue* ps);//对头删除数据

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

int QueueSize(Queue* ps);//测量队列的元素个数
bool QueueEmoty(Queue* ps);//判断队列是否为空
Queue.c
cpp 复制代码
#include"Queue.h"
void QueueInit(Queue* ps)//队列的初始化
{
	assert(ps);
	ps->ptail=ps->phead = NULL;
	ps->size = 0;
}
void QueuePush(Queue* ps,QDataType x)//队尾的插入
{
	assert(ps);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
			return;
	}
	newnode->next = NULL;
	newnode->val = x;
	if (ps->phead == NULL)
	{
		ps->phead = ps->ptail = newnode;
	}
	else
	{
		ps->ptail->next = newnode;
		ps->ptail = newnode;
	}
	ps->size++;
}
void QueuePop(Queue* ps)//队头的删除
{
	assert(ps);
	assert(ps->size != 0);
	if (ps->phead == ps->ptail)
	{
		ps->phead = ps->phead->next;
		free(ps->ptail);
		ps->ptail = NULL;
	}
	else
	{
		QNode* newhead = ps->phead;
		ps->phead = ps->phead->next;
		free(newhead);
		newhead = NULL;
	}
	ps->size--;
}
QDataType QueueFront(Queue* ps)//取出队头数据
{
	assert(ps);
	assert(ps->size != 0);
	return ps->phead->val;
}
QDataType QueueBack(Queue* ps)//取出队尾数据
{
	assert(ps);
	assert(ps->size != 0);
	return ps->ptail->val;
}
int QueueSize(Queue* ps)//测量队列数据个数
{
	assert(ps);
	return ps->size;
}
bool QueueEmoty(Queue* ps)//判断队列是否为空
{
	assert(ps);
	if (ps->phead == NULL)
	{
		return true;
	}
	return false;
}
void QueueDestroy(Queue* ps)//销毁队列
{
	assert(ps);
	QNode* pcur = ps->phead;
	QNode* pnext = pcur->next;
	while (pnext)
	{
		free(pcur);
		pcur = pnext;
		pnext = pnext->next;
	}
	free(pcur);
	ps->size = 0;
	ps->phead = NULL;
	ps->ptail = NULL;
}
test.c
cpp 复制代码
#include"Queue.h"
void test01()
{
	Queue s;
	QueueInit(&s);
	QueuePush(&s,1);
	QueuePush(&s,2);
	QueuePush(&s,3);
	QueuePush(&s,4);
	QueueDestroy(&s);
	//QueuePop(&s);
	//QueuePop(&s);
	//QueuePop(&s);
	//QueuePop(&s);
	printf("%d\n", QueueFront(&s));
	printf("%d\n", QueueBack(&s));
}
int main()
{
	test01();
	return 0;
}

值得注意的是:test.c只是我在测试函数功能时创建的。小伙伴们自行看看即可。

三.结言

文章到这里就结束了,觉得本文章对自己有用的小伙伴,能不能给个三连。

当然,如本文有些许错误,也请大捞们指正。

相关推荐
散峰而望1 小时前
【基础算法】高精度运算深度解析与优化
数据结构·c++·算法·链表·贪心算法·推荐算法
C雨后彩虹1 小时前
中文分词模拟器
java·数据结构·算法·华为·面试
Remember_9932 小时前
【LeetCode精选算法】二分查找专题二
java·数据结构·算法·leetcode·哈希算法
学嵌入式的小杨同学2 小时前
【嵌入式 C 语言实战】栈、队列、二叉树核心解析:存储原理 + 应用场景 + 实现思路
linux·c语言·网络·数据结构·数据库·后端·spring
2401_841495642 小时前
【LeetCode刷题】删除链表的倒数第N个结点
数据结构·python·算法·leetcode·链表·遍历·双指针
苦藤新鸡3 小时前
28.两数相加,进位制
数据结构·算法·链表·力扣
无限进步_3 小时前
二叉搜索树(BST)详解:从原理到实现
开发语言·数据结构·c++·ide·后端·github·visual studio
wangjialelele3 小时前
二刷C语言后,一万字整理细碎知识点
c语言·开发语言·数据结构·c++·算法·cpp
Stardep3 小时前
算法入门21——二分查找算法——山脉数组的峰顶索引
数据结构·算法·leetcode
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——力扣 675 题:为高尔夫比赛砍树
数据结构·算法·c·结构与算法