先上图,以图服人:

图一 程序运行截图1

图二 程序运行截图2

上代码:

头文件LinkQueue.h:

/*LinkQueue.h*/#ifndef LINKQUEUE_H_INCLUDED
#define LINKQUEUE_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>
#include <time.h>
#include <windows.h>typedef int LQElementType;
typedef struct Node
{LQElementType Data;struct Node *Next;
}LNode,*PNode;typedef struct LinkQueue
{LNode *Front,*Rear;}LQueue,*PQueue;PQueue InitiateQueue(PQueue LQ);/*初始化队列*/
void DestroyQueue(PQueue LQ);/*销毁队列*/
void Display(PQueue LQ);/*打印队列:边出队边打印队首元素,不推荐使用*/
void ShowQueue(PQueue LQ);/*打印队列2*/
bool ClearQueue(PQueue LQ);/*清空队列*/
bool EmptyQueue(PQueue LQ);/*判断队列是否为空*/
bool EnQueue(PQueue LQ,LQElementType Value);/*队尾入队*/
bool DeQueue(PQueue LQ,LQElementType *Tmp);/*队首出队*/
bool GetFront(PQueue LQ,LQElementType *Tmp);/*获取队首元素*/
int QueueSize(PQueue LQ);/*返回队列长度*/#endif // LINKQUEUE_H_INCLUDED

源文件test.c:

/*test.c*//*Name:纯C语言实现链式队列的相关操作Copyright:2018Author:刁肥宅Date: 06/08/18 17:49Description:一切尽在注释中!
*/#include "LinkQueue.h"int main()
{srand(time(NULL));LQueue *LQ = (LQueue *)malloc(sizeof(LQueue)),*LQ2 = NULL;int Value,i,QueueLength,Line = 0,FirstKey = -1;int DeQueueFrequency = 0,EnQueueFrequency = 0,ChoosingKey,RandomEnQueueValue = 0,RandomOperatingSize = 0;LQ = InitiateQueue(LQ);LQ2 = LQ;QueueLength = rand() % 1000 + 1;for(i = 0;i < QueueLength;i ++){Value = rand() % 10000 + 1;/*Value = 100;*/if(EnQueue(LQ,Value))continue;elsebreak;}for(i = 0;i < RandomOperatingSize;i ++){ChoosingKey = rand() % 2 + 0;if(!ChoosingKey){if(DeQueue(LQ2,&FirstKey))DeQueueFrequency ++;/*elsecontinue;*/}else{RandomEnQueueValue = rand() % 10000 + 1;if(EnQueue(LQ2,RandomEnQueueValue))EnQueueFrequency ++;}}int n = QueueSize(LQ);printf("QueueSize() = %d\n",n);ShowQueue(LQ);printf("\n\n");/*Display(LTmp);*/DeQueue(LQ,&FirstKey);printf("FirstKey = %d\n",FirstKey);if(GetFront(LQ,&FirstKey))printf("After DeQueue,FirstKey = %d\n\n",FirstKey);elseprintf("GetFront Error.\n");printf("EnQueueFrequency = %d,DeQueueFrequency = %d\n",EnQueueFrequency,DeQueueFrequency);ClearQueue(LQ);int n2 = QueueSize(LQ);printf("QueueSize() = %d\n",n2);DestroyQueue(LQ);int n1 = QueueSize(LQ);printf("QueueSize() = %d\n",n1);if(LQ->Front->Next == NULL)printf("DestroyQueue Succeed\n");else{if(EmptyQueue(LQ))printf("Empty\n");elseprintf("Not Empty\n");}/*LQ2 = InitiateQueue(LQ2);RandomOperatingSize = rand() % 1000 + 1;for(i = 0;i < RandomOperatingSize;i ++){Value = rand() % 1000 + 1;if(EnQueue(LQ2,Value))continue;elsebreak;}for(i = 0;i < RandomOperatingSize;i ++){ChoosingKey = rand() % 2 + 0;if(!ChoosingKey){if(DeQueue(LQ,&FirstKey))DeQueueFrequency ++;elsecontinue;}else{RandomEnQueueValue = rand() % 10000 + 1;if(EnQueue(LQ,RandomEnQueueValue))EnQueueFrequency ++;}}n2 = QueueSize(LQ2);printf("QueueSize() = %d\n",n2);if(GetFront(LQ2,&FirstKey))printf("Key = %d\n\n",FirstKey);elseprintf("GetFront Error.\n");ShowQueue(LQ2);printf("EnQueueFrequency = %d,DeQueueFrequency = %d\n",EnQueueFrequency,DeQueueFrequency);*/printf("\n");printf("The screen will be closed in 60 seconds.\n");Sleep(1000 * 60);return 0;
}PQueue InitiateQueue(PQueue LQ)
{LQ->Front = LQ->Rear = (LNode *)malloc(sizeof(LNode));/*LQ->Front = LQ->Rear;*/if(!LQ->Front){printf("Initiate failed.\n");exit(EXIT_FAILURE);}LQ->Front->Next = NULL;return LQ;
}bool EmptyQueue(PQueue LQ)
{return LQ->Front == LQ->Rear;
}bool EnQueue(PQueue LQ,LQElementType Value)
{LNode *QueueNodeTmp = (LNode *)malloc(sizeof(LNode));QueueNodeTmp->Next = NULL;QueueNodeTmp->Data = Value;/*if(QueueEmpty(LQ))*/if(!LQ->Rear)LQ->Front = LQ->Rear = QueueNodeTmp;else{LQ->Rear->Next = QueueNodeTmp;LQ->Rear = QueueNodeTmp;}return true;
}bool DeQueue(PQueue LQ,LQElementType *Tmp)
{if(!LQ->Front)return false;/*LNode *NodeTmp = (LNode *)malloc(sizeof(LNode));NodeTmp = LQ->Front;*Tmp = LQ->Front->Next->Data;*/LNode *NodeTmp = LQ->Front;*Tmp = NodeTmp->Next->Data;/*上面这句话我原来写作:*Tmp = LQ->Front->Data;等价于:*Tmp = NodeTmp->Data;导致 Display 函数 调用 DeQueue 函数时,第一个打印出来的总是随机数值。原因:第一次加载数据没有走EnQueue 函数的 if 条件语句,然后 LQ->Front->Data 就没值(不指向Next无法识别数据域)症结:不理解指针、队列的某些基本概念*/LQ->Front = NodeTmp->Next;if(NodeTmp == LQ->Rear)LQ->Front = LQ->Rear;free(NodeTmp);if(!LQ->Front)LQ->Rear = NULL;/*NodeTmp = NULL;*/return true;
}void DestroyQueue(PQueue LQ)
{LNode *Tmp = (LNode *)malloc(sizeof(LNode));/*solution 1:*/while(LQ->Front != LQ->Rear){Tmp = LQ->Front->Next;free(LQ->Front);LQ->Front = Tmp;}/*solution 2(badly function):while(LQ->Front != NULL){Tmp = LQ->Front;LQ->Front = Tmp->Next;free(Tmp);}*/LQ->Front->Next = LQ->Rear->Next = NULL;/*LQ->Front = LQ->Rear;*/
}bool ClearQueue(PQueue LQ)
{if(EmptyQueue(LQ))return false;LQ->Front = LQ->Rear;return true;
}void Display(PQueue LQ)
{int x = -1,Line = 0;if(LQ->Front == LQ->Rear){printf("Empty Queue.\n");exit(EXIT_FAILURE);}while(LQ->Front != LQ->Rear){/*printf("%-6d",LQ->Front->Data);LQ = LQ->Front;*/if(DeQueue(LQ,&x)){printf("%-6d",x);++ Line;if(Line % 10 ==0)printf("\n");}}
}void ShowQueue(PQueue LQ)
{/*LNode *Tmp = (LNode *)malloc(sizeof(LNode));Tmp = LQ->Front->Next;*/LNode *Tmp = LQ->Front->Next;int Line = 0;while(Tmp){printf("%-6d",Tmp->Data);++ Line;if(Line % 10 == 0)printf("\n");Tmp = Tmp->Next;}
}bool GetFront(PQueue LQ,LQElementType *Tmp)
{if(!LQ->Front)return false;*Tmp = LQ->Front->Next->Data;return true;
}int QueueSize(PQueue LQ)
{if(!LQ->Front)return -1;LNode *Tmp = LQ->Front;int ID = 0;while(LQ->Rear != Tmp){Tmp = Tmp->Next;++ ID;}return ID;
}

刁肥宅手笔:纯C语言实现链式队列的相关操作相关推荐

  1. 刁肥宅手笔:纯C语言实现栈的相关操作

    有一种爱不解释--我爱C语言! /*Name:纯C语言实现栈的相关操作Copyright:欢迎共享此代码Author:刁肥宅Date: 04/08/18 21:55Description:调试过程出行 ...

  2. 刁肥宅手笔:纯C语言利用链栈实现从后缀表达式Array中顺序输入表达式并求值

    链栈+后缀表达式求值算法,借用我自己上次C语言写的链栈,详细实现代码如下: 头文件C_Stack.h: /*C_Stack.h*/#ifndef C_STACK_H_INCLUDED #define ...

  3. c语言定义链式队列用菜单,数据结构之---C语言实现链式队列

    //链式队列的存储 //杨鑫 #include #include typedef int QElemType; //定义节点 typedef struct QNode { QElemType data ...

  4. 刁肥宅数据结构课设:布隆过滤器的实现和应用(v 1.1,修正非最终版)

    项目代码和报告的GitHub地址:https://github.com/25thengineer/Data-structure-course-design-The-Implementations-an ...

  5. 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现

    1. 队列的定义 所谓队列(queue)就是一种能实现"先进先出"的一种线性存储结构. 跟栈有点类似,  例如栈只有1个出入口, 任何元素进入或者离开栈都必须经过同1个出入口(栈顶 ...

  6. c语言建立队列(顺序队列、循化队列和链式队列)

    c语言建立队列 一.顺序队列 队列的顺序存储结构 顺序队列的讨论 "下溢"现象 "真上溢"现象 "假上溢"现象 二.如何解决"假上 ...

  7. 数据结构——链式队列解析(C语言版)

    摘自:数据结构学习--链式队列解析(C语言版) 作者:正弦定理 发布时间:2020-11-26 21:07:08 网址:https://blog.csdn.net/chinesekobe/articl ...

  8. 【R语言中如何去除替换NA相关操作】

    R语言中如何去除替换NA相关操作 1.去除矩阵所有含NA的行 2.去除矩阵特定列中含NA的行 3.替换矩阵中的NA值为0 4.将矩阵中某一列的特殊值替换为NA 1.去除矩阵所有含NA的行 data=n ...

  9. 一拖再拖,刁肥宅个人主页终上线!

    模板在此.侵删! 个人主页仅用于展示个人信息,无任何商业目的! Projects仍使用原作者的信息,并未改动,将会于约一周后修改为本人信息. 域名目前在进行备案中,不日上线. 图1 Home 图2 A ...

最新文章

  1. 基于Gitolite的Git服务架设
  2. C++实现二分查找(递归方法和非递归方法)
  3. 剑指offer-栈的压入、弹出序列
  4. wxWidgets:运行时类型信息 (RTTI)
  5. 课堂练习 5-22 团队如何做决定
  6. vmware中nat模式中使用静态ip后无法上网的问题
  7. c语言 格式转换函数,C语言中的格式转换函数.doc
  8. Power BI 与 Azure Analysis Services 的数据关联:4、Power BI 连接到Azure Analysis Services 并展示...
  9. 鸿蒙2.0内测版手机版,不再遮遮掩掩,鸿蒙2.0测试版下月发布,用于手机内测
  10. ios6.x 插件介绍及常用源
  11. HTML做一个学校网站(纯html代码)
  12. 汉王考勤机管理系统服务器,汉王考勤管理系统7
  13. 什么是驻点和拐点_拐点和驻点的区别有哪些
  14. (附源码)计算机毕业设计ssm电商后台管理系统
  15. 1147 Heaps (30分)
  16. Android脱壳圣战---360脱壳与修复
  17. vue基于vant实现上拉加载下拉刷新
  18. Acwing-873. 欧拉函数
  19. 常见的一些代码编辑器
  20. 删除vlan 华为s5720_华为S5720系列交换机快速配置手册常用命令

热门文章

  1. linux 进入recovery模式,recovery模式怎么进入,教您recovery模式怎么进入
  2. jQuery学习笔记(二)使用选择器一
  3. 滴答顺风车怎么抢90%以上的订单_想来赚顺风车钱的补课内容都给你准备好了
  4. 小米android手机密码忘了怎么解锁,小米手机锁屏密码忘了怎么解锁?
  5. matlab 召回率,查准率、召回率、敏感性、特異性和F1-score的計算及Matlab實現
  6. android点击按钮发出声音
  7. 汇编语言的AX,BX,CX,DX,分别表示什么
  8. 2018年10大流行Python库
  9. 戴尔服务器安装独显后无显示,在T630服务器上安装了独立显卡,重启后液晶面板显示“pci1318 fatal error on bus 128d”,然后黑屏重启,该问题如何解决阿。...
  10. FT232RL USB串口与GP232RL软硬件兼容开发资料