首页 > 其他分享 >数据结构:单链队列--队列的链式存储结构

数据结构:单链队列--队列的链式存储结构

时间:2022-12-07 16:02:44浏览次数:41  
标签:单链 return 队列 LinkQueue next -- front rear


//测试

数据结构:单链队列--队列的链式存储结构_链队列


<div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;">/*************************************************************</div><div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;">
</div><div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;"><span style="margin: 0px; padding: 0px; white-space: pre;"> </span>程序:单链队列--队列的链式存储结构</div><div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;"><span style="margin: 0px; padding: 0px; white-space: pre;"> </span>完成者:小单</div><div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;">
</div><div style="margin: 0px; padding: 0px; font-family: punctuation, 微软雅黑, Tohoma; font-size: 14px; line-height: 22px;">***************************************************************/</div><pre name="code" class="cpp">

#include <stdio.h>#include <stdlib.h>#define OVERFLOW -2#define OK 1#define ERROR 0#define FALSE 0#define TRUE 1typedef int Status;typedef int QElemType;typedef struct QNode{QElemType data;struct QNode *next;}QNode, *QueuePtr;typedef struct {QueuePtr front; //队头指针QueuePtr rear; //队尾指针}LinkQueue;Status InitQueue(LinkQueue &Q)//构造一个空队列{Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));//队头结点if(!Q.front)exit(OVERFLOW);Q.front ->next = NULL;return OK;}Status ClearQueue(LinkQueue &Q) //将Q清为空队列{if(Q.front == Q.rear)return OK;/*free(Q.front->next); Q.rear = Q.front;Q.front->next = NULL;*/QueuePtr p, q;p = Q.front->next;while(p){q = p;p = p->next;free(q);}Q.rear = Q.front;Q.front->next = NULL;return OK;}Status DestroyQueue(LinkQueue &Q)//销毁队列Q{ClearQueue(Q);if(Q.front){free(Q.front);}Q.front = Q.rear = NULL;return OK;}Status QueueEmpty(const LinkQueue &Q)//若队列为空,则返回TRUE,否则返回FALSE{if(Q.rear == Q.front)return TRUE;return FALSE;}int QueueLength(const LinkQueue &Q){//return Q.rear - Q.front;QueuePtr p;p = Q.front;int cnt=0;while(p->next){++cnt;p = p->next;}return cnt;}Status GetHead(const LinkQueue &Q, QElemType &e)//若队空,则返回ERROR,否则有e返回值{if(Q.front == Q.rear)return ERROR; //队空,返回出错提示e = Q.front->next->data; //队不空,返回队头的元素return OK;}Status EnQueue(LinkQueue &Q, QElemType e) //插入元素e为Q的新队尾元素{QueuePtr p = (QueuePtr)malloc(sizeof(QNode));if(!p)exit(OVERFLOW);p->data = e;p->next = NULL;Q.rear->next = p;//Q.rear = p;return OK;}Status DeQueue(LinkQueue &Q,QElemType &e) //若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;{if(Q.front == Q.rear){return ERROR; //队空}QueuePtr p = Q.front->next;e = p->data;Q.front->next = p->next;if(Q.rear == p)Q.rear = Q.front;free(p);return OK;}int main()//测试{LinkQueue Q;InitQueue(Q); //初始化一个空队for(int i = 0; i < 3; ++i)EnQueue(Q,i+1); //入队printf("长度为%d\n",QueueLength(Q));//求队中的元素QElemType e;if(GetHead(Q,e)) //求队头元素{printf("队头元素为:%d\n",e);}else{printf("队空\n");}//ClearQueue(Q); //清空队列/*if(DestroyQueue(Q)) //销毁队列{printf("队已销毁\n");}*/while(DeQueue(Q,e))//依次出队{printf("%3d",e);}printf("\n");ClearQueue(Q); //清空队列if(QueueEmpty(Q)){printf("队空\n");}if(DestroyQueue(Q)) //销毁队列{printf("队已销毁\n");}return 0;}





标签:单链,return,队列,LinkQueue,next,--,front,rear
From: https://blog.51cto.com/u_15905375/5919629

相关文章

  • Xshell断开连接后仍保持服务器程序执行的方法
    nohup(参考https://blog.csdn.net/limiaoiao/article/details/81948401,实现Xshell断开连接情况下Linux命令继续执行)1、将原命令语句改为:nohup命令语句&2、回车执行,再......
  • Angular RouterModule.forRoot(ROUTES) 和 forChild(ROUTES)的区别
    不少Angular初学者在学习Angular路由框架时,对forRoot和forChild这两个方法的差异都心生疑惑。Angular官网对两个方法的解释:forRoot创建一个包含所有指令、给......
  • 什么swtich中case没有break不行?
    前言一个小姐姐拿着一个switch的选择题来问我。之所以这么笃定地回答这个问题,并不是我知道其中原理,而是之前在一个群里,有人问了同类型的问题,我瞥了一眼记住了答案,所以才......
  • 算法练习:两数之和
    题目:给定一个整型数组,是否能找出两个数使其和为指定的某个值?注:整型数组中不存在相同的数。一、解题方法1、暴力解法(时间复杂度O(n^2) )这是最容易想到的一种方法,即使用两......
  • 建造者模式(描述语言PHP)
    Builder模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的结果对象,一般来说Builder模式中对象不是直接返回的。 <?php/***建造者模式**将一个复杂......
  • 数据结构:静态查找表(线性链表)
    /***********************************************************     程序:静态查找表(线性链表)*************************......
  • 关于MFC中resource.h头文件中宏的说明
    在写MFC程序时,当需要动态创建一些控件的时候,需要传递一个ID给相应的控件,比如创建一个按钮CButtonm_bnTestButton;m_bnTestButton.Create(_T("我的按钮"),WS_VISIBLE|WS_C......
  • 001- hive文件存储格式
    1.文件存储格式TextFileSequeceFileRCFileORCFilePARQuet2.说明其中TEXTFILE为默认格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理;SequenceFile,RCF......
  • Scrapy入门到放弃01:我为什么选择Scrapy
    前言Scrapyiscoming!!在写了七篇爬虫基础文章之后,终于写到心心念念的Scrapy了。Scrapy开启了爬虫2.0的时代,让爬虫以一种崭新的形式呈现在开发者面前。在18年实习的时候......
  • GDIplus的初次接触--加载并显示常用格式图片
     在没有接触Gdiplus之前,在vc中绘制图片,通常加载一张位图,然后进行贴图。对于现在多种多样的图片格式,之前的GDI并不支持(应该是这样的,呵呵)。而使用Gdiplus则可以选择多种图片......