0
点赞
收藏
分享

微信扫一扫

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

Alex富贵 2022-12-07 阅读 113


//测试

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


<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;}





举报

相关推荐

0 条评论