链队列

news/2024/7/2 22:18:08

 

#include<bits/stdc++.h>
#define OK 1
#define ERROR 0
using namespace std;typedef int Status;
typedef int ElemType;typedef struct QNode
{ElemType data;struct QNode *next;
}*Node;typedef struct
{Node front;Node rear;int len;
}LinkQueue;
///初始化
Status Init(LinkQueue &Q)
{Node p;p=(Node)malloc(sizeof(QNode));if(p){Q.front=Q.rear=p;p->next=NULL;Q.len=0;return OK;}elsereturn ERROR;
}
///判断队列是否为空
bool EmptyQueue(LinkQueue Q)
{if(Q.front == Q.rear){return true;}else{return ERROR;}
}
///入队
Status Push(LinkQueue &Q, ElemType e)
{Node p;p=(Node)malloc(sizeof(QNode));if(!p)exit(1);p->data=e;p->next=NULL; ///注意此处,刚开始没有想到,只有 p->next = NULL 才确定p是尾部Q.rear->next=p;Q.rear=p;Q.len++;return OK;
}
///出队
int Pop(LinkQueue &Q,ElemType &e)
{if(Q.front == Q.rear){printf("链队列没有元素\n");return ERROR;}else{Node p;p=Q.front->next;e=p->data;Q.front->next=p->next;if(Q.rear ==  p)Q.rear=Q.front;free(p);Q.len--;return e;}
}
///求队列长度
int QueueLength(LinkQueue Q)
{return Q.len;
}
///销毁队列
Status DestroyQueue(LinkQueue &Q)
{while(Q.front){Q.rear=Q.front->next;free(Q.front);Q.front=Q.rear;}return OK;
}///清空队列
Status ClearQueue(LinkQueue &Q)
{DestroyQueue(Q);Init(Q);return OK;
}/*另一种思路写法
Status ClearQueue(LinkQueue &Q)
{Node p,q;p=Q.front->next;while(p){q=p;p=p->next;free(q);}Q.rear=Q.front;Q.len=0;return OK;}另一种思路写法*/Status Print(LinkQueue Q)
{Node p;p=Q.front->next;while(p!=Q.rear){printf("%d--",p->data);p=p->next;}printf("%d\n",Q.rear->data);return OK;
}int main()
{int n;LinkQueue Q;Init(Q);printf("请输入入队元素(以0为结束标志):");while(~scanf("%d",&n)){if(n == 0)break;Push(Q,n);}if(EmptyQueue(Q))printf("YES,kong\n");elseprintf("NO kong\n");printf("队列的长度为:%d\n",QueueLength(Q));printf("遍历队首到队尾的元素:\n");Print(Q);printf("------------------------------\n");int e;printf("出队的队首元素为:%d\n",Pop(Q,e));printf("队列的长度为:%d\n",QueueLength(Q));printf("遍历队首到队尾的元素:\n");Print(Q);printf("------------------------------\n");printf("Clear queue...\n");ClearQueue(Q);if(EmptyQueue(Q))printf("YES,kong\n");elseprintf("NO kong\n");printf("------------------------------\n");return 0;
}
/*
1 2 3 4 5 0
*/

 

转载于:https://www.cnblogs.com/mcgrady_ww/p/7898843.html


http://lihuaxi.xjx100.cn/news/239684.html

相关文章

C语言图书管理系统注册功能,图书管理系统的c语言源程序

/*****************************************************************************************/#include #include #include #include /输入/输出文件流类using namespace std;const int maxr100;/最多的读者const int maxb100;/最多的图书const int maxbor5;/每位读者最多借…

初学者css常见问题_5分钟内学习CSS Grid-初学者教程

初学者css常见问题Grid layouts are fundamental to the design of websites, and the CSS Grid module is the most powerful and easiest tool for creating it. I personally think it’s a lot better than for example Bootstrap (read why here).网格布局是网站设计的基础…

配置文件的简单使用

常见的配置文件格式:1.properties里面内容的格式 keyvalue2.xml后期详细解释若我们的配置文件为properties,并且放在src目录下.我们可以通过 ResourceBundle工具快速获取里面的配置信息使用步骤:1.获取ResourceBundle 对象:static ResourceBundle getBundle("文件名称不带…

解决:无法创建该DNS 服务器的委派

第一次安装AD DNS的时候&#xff0c;你可能遇到以下的提示&#xff0c;无法创建该DNS 服务器的委派&#xff0c;这是一个提示&#xff0c;而不是一个报错。 以下是详细的说明。 将具有 DNS 服务器的新 Windows Server 2008 或 Windows Server 2008 R2 域控制器安装到 treyr…

npm构建脚本_NPM脚本简介

npm构建脚本by Mohammed Ajmal Siddiqui由Mohammed Ajmal Siddiqui NPM脚本简介 (Introduction to NPM Scripts) NPM scripts are among my favorite features of NPM. They are simple. They reduce the need for tools. Hence they reduce the number of configuration file…

String、StringBuffer与StringBuilder之间区别 (转载)

最近学习到StringBuffer&#xff0c;心中有好些疑问&#xff0c;搜索了一些关于String&#xff0c;StringBuffer&#xff0c;StringBuilder的东西&#xff0c;现在整理一下。 关于这三个类在字符串处理中的位置不言而喻&#xff0c;那么他们到底有什么优缺点&#xff0c;到底什…

鸿蒙系统能内测吗,鸿蒙系统内测用户:使用体验已经超越ios

首先&#xff0c;鸿蒙流畅度堪比iOS&#xff0c;如同德芙巧克力一样非常丝滑&#xff0c;各种界面切换毫无卡顿。鸿蒙是万物互联的基础&#xff0c;在与其他设备联动上&#xff0c;使用了freebuds pro和magic wathc2&#xff0c;状态栏就能直接切换耳机&#xff0c;非常顺畅&am…

SQL to Elasticsearch java code

把Elasticsearch当成Database用&#xff0c;因为Elasticsearch不支持SQL&#xff0c;就需要把SQL转换成代码实现。 1.按某个field group by查询count SELECT fieldA, COUNT(fieldA) from table WHERE fieldC "hoge" AND fieldD "huga" AND fieldB…