学校网站建设发展历程,长沙网站建设王道下拉惠,网页美术设计主要学什么,上海哪家网站建得好 作者简介#xff1a;დ旧言~#xff0c;目前大一#xff0c;现在学习Java#xff0c;c#xff0c;c#xff0c;Python等 座右铭#xff1a;松树千年终是朽#xff0c;槿花一日自为荣。 望小伙伴们点赞#x1f44d;收藏✨加关注哟#x1f495;#x1… 作者简介დ旧言~目前大一现在学习JavaccPython等 座右铭松树千年终是朽槿花一日自为荣。 望小伙伴们点赞收藏✨加关注哟 前言 前面我们已经学习了顺序表和链表他们无法控制数据的打印而队列只允许在一端进行插入数据操作在另一端进行删除数据操作的特殊线性表队列具有先进先出 FIFO(First In First Out) 入队列进行插入操作的一端称为队尾 出队列进行删除操作的一端称为队头今天我们来实现一下--《队列》。 主体
咱们从两个方面实现队列动态管理对元素进行操作。 在程序中为了实现队列需要创建头文件Queue.h 创建源文件Queue.c这里没有主函数了等到二叉树的时候就会再次运用到队列。 动态管理
初始化动态队列
1.首先我们在Queue.h定义动态的队列省得我们再调用队列这里和链表是一样哒。
//实现队列
typedef struct Queue
{//头QNode* head;//尾QNode* tail;int size;
}Que;
2.对队列进行初始化没啥好说的。
//初始化
void QueueInit(Que* pq)
{//断言assert(pq);//初始化pq-head pq-tail NULL;pq-size 0;
} 释放队列内存
这里采用循环的形式来释放内存都写烂啦。 //销毁
void QueueDestroy(Que* pq)
{//断言assert(pq);//每个节点销毁QNode* cur pq-head;while (cur){QNode* next cur-next;//释放内存free(cur);cur next;}pq-head pq-tail NULL;pq-size 0;
} 对元素进行操作 添加元素重点
这里以单链表的元素进入队列实现先进先出。 //添加元素
void QueuePush(Que* pq, QDataType x)
{//断言assert(pq);//开辟空间QNode* newnode (QNode*)malloc(sizeof(QNode));//判断if (newnode NULL){perror(malloc fail);exit(-1);}newnode-data x;newnode-next NULL;//当尾没有元素时 把头当做尾if (pq-tail NULL){pq-head pq-tail newnode;}else{pq-tail-next newnode;pq-tail newnode;}pq-size;
} 释放元素出队列重点
这里遵循先进先出就行。 void QueuePop(Que* pq)
{//断言 元素不能为0assert(pq);assert(!QueueEmpty(pq));//如果只有一个元素 就头尾一起删if (pq-head-next NULL){//释放内存free(pq-head);pq-head pq-tail NULL;}else{//头删QNode* next pq-head-next;//释放内存free(pq-head);pq-head next;}pq-size--;
} 找头
这个函数没啥好说的直接返回头节点的元素值。
//找尾
QDataType QueueBack(Que* pq)
{//断言assert(pq);assert(!QueueEmpty(pq));//找尾return pq-tail-data;
} 找尾
这个函数没啥好说的直接返回尾节点的元素值。
//找尾
QDataType QueueBack(Que* pq)
{//断言assert(pq);assert(!QueueEmpty(pq));//找尾return pq-tail-data;
} 计算队列元素个数
这个函数没啥好说的直接返回pq-size。
//计算队列元素个数
int QueueSize(Que* pq)
{assert(pq);return pq-size;
}
代码总结
Queue.h头文件
//包含头文件
#includestdio.h
#includestdlib.h
#includeassert.h
#includestdbool.h//定义元素类型
typedef int QDataType;//定义节点
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;//实现队列
typedef struct Queue
{//头QNode* head;//尾QNode* tail;int size;
}Que;//初始化
void QueueInit(Que* pq);
//销毁
void QueueDestroy(Que* pq);//添加元素
void QueuePush(Que* pq, QDataType x);
//删除元素
void QueuePop(Que* pq);//找头
QDataType QueueFront(Que* pq);
//找尾
QDataType QueueBack(Que* pq);//判断
bool QueueEmpty(Que* pq);
//计算队列元素个数
int QueueSize(Que* pq);Queue.c源文件
//包含头文件
#includeQueue.h//初始化
void QueueInit(Que* pq)
{//断言assert(pq);//初始化pq-head pq-tail NULL;pq-size 0;
}//销毁
void QueueDestroy(Que* pq)
{//断言assert(pq);//每个节点销毁QNode* cur pq-head;while (cur){QNode* next cur-next;//释放内存free(cur);cur next;}pq-head pq-tail NULL;pq-size 0;
}//添加元素
void QueuePush(Que* pq, QDataType x)
{//断言assert(pq);//开辟空间QNode* newnode (QNode*)malloc(sizeof(QNode));//判断if (newnode NULL){perror(malloc fail);exit(-1);}newnode-data x;newnode-next NULL;//当尾没有元素时 把头当做尾if (pq-tail NULL){pq-head pq-tail newnode;}else{pq-tail-next newnode;pq-tail newnode;}pq-size;
}//删除元素
void QueuePop(Que* pq)
{//断言 元素不能为0assert(pq);assert(!QueueEmpty(pq));//如果只有一个元素 就头尾一起删if (pq-head-next NULL){//释放内存free(pq-head);pq-head pq-tail NULL;}else{//头删QNode* next pq-head-next;//释放内存free(pq-head);pq-head next;}pq-size--;
}//找头
QDataType QueueFront(Que* pq)
{//断言assert(pq);assert(!QueueEmpty(pq));//找头return pq-head-data;
}//找尾
QDataType QueueBack(Que* pq)
{//断言assert(pq);assert(!QueueEmpty(pq));//找尾return pq-tail-data;
}//判断
bool QueueEmpty(Que* pq)
{//断言assert(pq);return pq-head NULL;
}//计算队列元素个数
int QueueSize(Que* pq)
{assert(pq);return pq-size;
}
结束语 今天内容就到这里啦时间过得很快大家沉下心来好好学习会有一定的收获的大家多多坚持嘻嘻成功路上注定孤独因为坚持的人不多。那请大家举起自己的小说手给博主一键三连有你们的支持是我最大的动力回见。