数据结构(队列及其实现)

概念与结构

概念:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表,

队列具有先进先出FIFO(First In First Out)原则。

⼊队列:进⾏插⼊操作的⼀端称为队尾

出队列:进⾏删除操作的⼀端称为队头

队列底层结构选型

队列也可以数组和链表的结构实现,使⽤链表的结构实现更优⼀些,因为如果使⽤数组的结构,出队列在数组头上出数据,效率会⽐较


队列的实现

Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.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 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);

Queue.c

此处代码与头文件中的代码一一对应。

初始化

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;//newnode
	}
	pq->size++;
}

队列判空

//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL && pq->ptail == NULL;
}

出队列(队头)

// 出队列,队头
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	//只有一个结点的情况,避免ptail变成野指针
	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;
}

取队头数据

//取队头数据
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);
	/*int size = 0;
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		size++ ;
		pcur = pcur->next;
	}
	return size;*/
	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;
	pq->size = 0;
}

以上便是本期队列的全部内容,感谢大家的观看与支持,这是激励我前进的不竭动力!

相关推荐

  1. 【Golang】实现简单队列(Queue)数据结构

    2024-07-23 03:28:02       48 阅读
  2. 数据结构——队列链式存储实现

    2024-07-23 03:28:02       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 03:28:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 03:28:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 03:28:02       45 阅读
  4. Python语言-面向对象

    2024-07-23 03:28:02       55 阅读

热门阅读

  1. 数据结构---二叉树

    2024-07-23 03:28:02       17 阅读
  2. [k8s源码]6.reflector

    2024-07-23 03:28:02       11 阅读
  3. NumPy冷知识66个

    2024-07-23 03:28:02       15 阅读
  4. 怀庄之醉是勾兑酒吗?

    2024-07-23 03:28:02       15 阅读
  5. 【开源库学习】libodb库学习(一)

    2024-07-23 03:28:02       14 阅读
  6. 【Apollo学习笔记】—— Cyber RT之创建组件, test ok

    2024-07-23 03:28:02       15 阅读
  7. Linux Shell 022-按日期清理文件

    2024-07-23 03:28:02       16 阅读
  8. Red Playing Cards (牛客多校2 I)

    2024-07-23 03:28:02       17 阅读
  9. Husky 入门

    2024-07-23 03:28:02       16 阅读
  10. ResNeSt

    ResNeSt

    2024-07-23 03:28:02      18 阅读
  11. 如何引入全局样式文件?

    2024-07-23 03:28:02       16 阅读
  12. 长短期记忆网络(LSTM)及其Python和MATLAB实现

    2024-07-23 03:28:02       19 阅读