西峡微网站开发,有赞小程序开发报价,泗洪县建设局网站,长沙地区网络优化设计方案文章目录 #x1f438;一、队列的概念及结构#x1f344;1、队列的概念定义#x1f344;2、动图演示 #x1f438;二、队列的实现#x1f438;三、链表结构队列详解#x1f34e;创建队列的结构⭕接口1#xff1a;定义结构体#xff08;QNode、Queue#xff09;⭕接口2… 文章目录 一、队列的概念及结构1、队列的概念定义2、动图演示 二、队列的实现三、链表结构队列详解创建队列的结构⭕接口1定义结构体QNode、Queue⭕接口2初始化QueueInit⭕接口3销毁QueueDestroy⭕接口4入队列QueuePush⭕接口5出队列QueuePop⭕接口6取队头数据QueueFront⭕接口7取队尾数据QueueBack⭕接口8获取队列大小QueueSize⭕接口9判空QueueEmpty 四、完整代码Queue.hQueue.cTest.c 一、队列的概念及结构
1、队列的概念定义 队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出FIFO(First In First Out) 入队列进行插入操作的一端称为队尾 出队列进行删除操作的一端称为队头 入队列进行插入操作的一端称为队尾出队列进行删除操作的一端称为队头 2、动图演示 可以想象成排队去食堂打饭前面先打完饭的就从队头先走了后来的就需要在后面队尾继续排队
二、队列的实现 队列也可以数组和链表的结构实现使用链表的结构实现更优一些因为如果使用数组的结构出队列在数组头上出数据效率会比较低。 三、链表结构队列详解 创建队列的结构 这里先创建三个文件 1️⃣Queue.h文件用于函数的声明 2️⃣Queue.c文件用于函数的定义 3️⃣Test.c文件用于测试函数 建立三个文件的目的 将队列作为一个项目来进行编写方便我们的学习与观察。 ⭕接口1定义结构体QNode、Queue 这里需要定义两个结构体QNode、Queue分别表示队列链表每个节点结构和整个队列链表结构 请看代码与注释
//自定义类型
typedef int QDataType;//队列链表每个节点结构
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;//整个队列链表结构
typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;⭕接口2初始化QueueInit
请看代码与注释
//初始化
void QueueInit(Queue* pq)
{//断言传入指针不为NULLassert(pq);pq-phead NULL;pq-ptail NULL;pq-size 0;
}⭕接口3销毁QueueDestroy
请看代码与注释
//销毁
void QueueDestroy(Queue* pq)
{//断言传入指针不为NULLassert(pq);QNode* cur pq-phead;while (cur){QNode* next cur-next;free(cur); //释放cur next;}pq-phead pq-ptail NULL;pq-size 0;
}⭕接口4入队列QueuePush
请看代码与注释
//入队列
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode (QNode*)malloc(sizeof(QNode));if (newnode NULL){perror(malloc fail\n);return;}newnode-data x;newnode-next NULL;if (pq-ptail NULL) //如果没有节点空队列{assert(pq-phead NULL);pq-phead pq-ptail newnode;}else //非空队列{pq-ptail-next newnode;pq-ptail newnode;}pq-size;
}⭕接口5出队列QueuePop
请看代码与注释
//出队列
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//1、一个节点if (pq-phead-next NULL){free(pq-phead);pq-phead pq-ptail NULL;}//2、多个节点else{//头删QNode* next pq-phead-next;free(pq-phead);pq-phead next;}pq-size--;
}⭕接口6取队头数据QueueFront
请看代码与注释
//获取队头数据
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq-phead-data;
}⭕接口7取队尾数据QueueBack
请看代码与注释
//获取队尾数据
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq-ptail-data;
}⭕接口8获取队列大小QueueSize
请看代码与注释
//获取队列大小
int QueueSize(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq-size;
}⭕接口9判空QueueEmpty
请看代码与注释
//判空
bool QueueEmpty(Queue* pq)
{assert(pq);//return pq-phead NULL pq-ptail NULL;return pq-size 0;
}四、完整代码
Queue.h
#pragma once
#includestdio.h
#includestdlib.h
#includeassert.h
#includestdbool.htypedef int QDataType;//队列链表每个节点
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}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);
//获取队列大小
int QueueSize(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);Queue.c
#includeQueue.h//初始化
void QueueInit(Queue* pq)
{assert(pq);pq-phead NULL;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\n);return;}newnode-data x;newnode-next NULL;if (pq-ptail NULL){assert(pq-phead NULL);pq-phead pq-ptail newnode;}else{pq-ptail-next newnode;pq-ptail newnode;}pq-size;
}//出队列
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));//1、一个节点if (pq-phead-next NULL){free(pq-phead);pq-phead pq-ptail NULL;}//2、多个节点else{//头删QNode* 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);assert(!QueueEmpty(pq));return pq-size;
}//判空
bool QueueEmpty(Queue* pq)
{assert(pq);//return pq-phead NULL pq-ptail NULL;return pq-size 0;
}Test.c
#includeQueue.h//入队列测试
void TestQueue1()
{Queue q;QueueInit(q);QueuePush(q, 1);QueuePush(q, 2);QueuePush(q, 3);QueuePush(q, 4);while (!QueueEmpty(q)){printf(%d , QueueFront(q));QueuePop(q);}printf(\n);QueueDestroy(q);
}//测试
void TestQueue2()
{Queue q;QueueInit(q);QueuePush(q, 1);QueuePush(q, 2);printf(Size:%d\n, QueueSize(q));while (!QueueEmpty(q)){printf(%d , QueueFront(q));QueuePop(q);}printf(\n);QueueDestroy(q);
}int main()
{//TestQueue1();//TestQueue2();return 0;
}这期内容相对比较简单希望烙铁们可以理解消化哦 总结 以上就是 【数据结构】队列—C语言版 的全部内容啦 本文章所在【数据结构与算法】专栏感兴趣的烙铁可以订阅本专栏哦 前途很远也很暗但是不要怕不怕的人面前才有路。 小的会继续学习继续努力带来更好的作品 创作写文不易还多请各位大佬uu们多多支持哦