广西建设学院网站,杭州外贸建站公司,信阳建网站,企业网站建设中在方案设计上详解C语言实现链队列~#x1f60e;前言#x1f64c;整体实现内容分析#x1f49e;预备小知识#x1f64c;1.链队列头文件编写#x1f64c;2.链队列功能文件#xff08;Queue.c #xff09;编写#xff1a;#x1f64c;1#xff09;初始化函数实现2#xff09;销毁函…
详解C语言实现链队列~前言整体实现内容分析预备小知识1.链队列头文件编写2.链队列功能文件Queue.c 编写1初始化函数实现2销毁函数实现3队尾插入元素函数实现4队头删除元素函数实现5获取队头元素函数实现6获取队尾元素函数实现7获取队列元素个数的函数实现8判空函数实现3.链队列测试文件编写总结撒花博客昵称博客小梦 最喜欢的座右铭全神贯注的上吧 作者简介一名热爱C/C算法等技术、喜爱运动、热爱K歌、敢于追梦的小博主 博主小留言哈喽各位CSDN的uu们我是你的博客好友小梦希望我的文章可以给您带来一定的帮助话不多说文章推上欢迎大家在评论区唠嗑指正觉得好的话别忘了一键三连哦 前言 哈喽各位友友们我今天又学到了很多有趣的知识现在迫不及待的想和大家分享一下我仅已此文手把手带领大家详解C语言实现链队列~ 结合链表的相关算法根据队列先进先出的结构特点实现我们链队列。都是精华内容可不要错过哟 整体实现内容分析 首先先编写我们的Queue.h文件的内容将需要的头文件和需要实现的功能函数做个声明。然后是Queue.c文件的编写主要是针对各个功能函数的一一实现最后是Test.c文件的编写测试我们功能函数是否有问题。 预备小知识 队列也可以数组和链表的结构实现使用链表的结构实现更优一些因为如果使用数组的结构出队列在数组头上出数据效率会比较低。 队列的结构示意图 1.链队列头文件编写
头文件的编写的整体思路分析 这里是有关头文件的编写和各种功能函数的声明首先用typedef关键字给存储数据类型取别名这样做的好处是以后想要改变队列的数据类型只需修改typedef int QDataType;里的int即可。定义一个结构体包括next指针和data。然后是各个功能函数声明 #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* head;QNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(Queue* pq);
//队尾入
void QueuePush(Queue* pq, QDataType);
//队头出
void QueuePop(Queue* pq);
//队头元素
QDataType QueueFront(Queue* pq);
//队尾元素
QDataType QueueBack(Queue* pq);
//队列元素个数
int QueueSize(Queue* pq);
//判空
bool QueueEmpty(Queue* pq);
2.链队列功能文件Queue.c 编写
1初始化函数实现
编写的整体思路分析 先用assert确保pq指针的有效性然头尾指针置为空。 //初始化
void QueueInit(Queue* pq)
{assert(pq);pq-head pq-tail NULL;
}2销毁函数实现
编写的整体思路分析 先用assert确保pq指针的有效性先定义一个指针cur记录head指针的位置然后用while循环进行一个个删除先定义一个next指向cur指针指向的下一个队列元素然后删掉cur指向的元素再让cur移动到下一个。 //销毁
void QueueDestory(Queue* pq)
{assert(pq);QNode* cur pq-head;while (cur){QNode* next cur-next;free(cur);cur next;}pq-head pq-tail NULL;}
3队尾插入元素函数实现
编写的整体思路分析 先用assert确保pq指针的有效性用malloc函数申请空间用if语句来判断空间是否开辟失败。然后让newnode-next置为空把数据放进去。如果是一开始时为空的则让pq-head pq-tail newnode;如果不为空则让之前的tail指向的节点与新节点相连。 //队尾入
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode (QNode*)malloc(sizeof(QNode));if (newnode NULL){printf(malloc fail\n);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;
4队头删除元素函数实现
编写的整体思路分析 用assert确保pq指针的有效性如果队列为空就不用进行队头出的操作如果执行则报错。先用next指针指向首结点的下一个节点 。然后删掉首节点再让head指针指向next位置。删完后将尾指针置为NULL //队头出
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));QNode* next pq-head-next;free(pq-head);pq-head next;if (pq-head NULL){pq-tail NULL;}
}
5获取队头元素函数实现
编写的整体思路分析 先用assert确保pq指针的有效性如果队列为空就不用进行此操作。然后直接返回首节点数据即可。 //队头元素
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq-head-data;
}
6获取队尾元素函数实现
编写的整体思路分析 先用assert确保pq指针的有效性如果队列为空就不用进行此操作。然后直接返回尾节点数据即可。 //队尾元素
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq-tail-data;}
7获取队列元素个数的函数实现
编写的整体思路分析 先用assert确保pq指针的有效性定义cur指向head的位置。利用while循环先让Size加1然后让cur指向下一个知道cur为空时结束循环返回Size大小就是队列元素的个数。 //队列元素个数
int QueueSize(Queue* pq)
{assert(pq);int Size 0;QNode* cur pq-head;while (cur){Size;cur cur-next;return Size;}
}
8判空函数实现
编写的整体思路分析 先用assert确保pq指针的有效性然后pq-head NULL为真则返回1为假则返回0 //判空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq-head NULL;
}
3.链队列测试文件编写
3Tese.c文件的编写。
#define _CRT_SECURE_NO_WARNINGS 1
#includeQueue.h
void test()
{Queue q;QueueInit(q);QueuePush(q, 1);QueuePush(q, 2);QueuePush(q, 3);QueuePush(q, 4);printf(队首元素%d\n, QueueFront(q));printf(队尾元素%d\n, QueueBack(q));printf(队列元素的个数%d\n, QueueBack(q));printf(队列元素输出);while (!QueueEmpty(q)){QDataType* front QueueFront(q);printf(%d, front);QueuePop(q);}printf(\n);QueueDestory(q);
}
int main()
{test();return 0;
}
功能测试结果展示图
总结撒花 本篇文章旨在分享详解C语言实现链队列。希望大家通过阅读此文有所收获本次关于队列的实现相对于之前链表和顺序表的实现简单一点实现起来的算法和之前的栈是相似的但结构特点与栈是相反的。指针的指向没有那么复杂主要是对入队列和出队列的功能实现。但也有很多地方需要注意的。比如说野指针的问题动态开辟的空间一定要free掉并且把指针置为NULL。用动态实现相对于静态实现还比较灵活也能对空间有很大的节省。 如果我写的有什么不好之处请在文章下方给出你宝贵的意见。如果觉得我写的好的话请点个赞赞和关注哦~
文章转载自: http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.rhmt.cn.gov.cn.rhmt.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.ahlart.com.gov.cn.ahlart.com http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.bzlfw.cn.gov.cn.bzlfw.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.sgmis.com.gov.cn.sgmis.com http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.lmmkf.cn.gov.cn.lmmkf.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.gmswp.cn.gov.cn.gmswp.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn