关于队列的程序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(flag==OK)printf("已成功创建空队列\n");
elseexit(OVERFLOW);
printf("请输入要入队的数据个数:");
scanf("%d",&n);
printf("\n请输入要入队的%d个数据:\n",n);
for(i=0;i<n;++i){
scanf("%d", &e);
//这里将队列元素定义为int类型,可根据实际应用的需要换成别的类型
typedefintQElemType;
//单链队列--队列的链式存储结构
typedefstructQNode{
QElemTypedata;
structQNode*next;
}QNode, *QueuePtr;
typedefstruct{
Q.front->next = NULL;
returnOK;
}
StatusDestroyQueue(LinkQueue&Q){
//销毁队列
while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
returnOK;
}
StatusClearQueue(LinkQueue&Q){
if(Q.front==Q.rear) return ERROR;
p =Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);
returnOK;
}
StatusPrint(QElemTypee){
p =Q.front->next;
while(p){
++n; p = p->next;
}
returnn;
}
StatusGetHead(LinkQueueQ,QElemType&e){
//若队列不空,则用e返回Q的队头元素,并并返回OK;否则返回ERROR
if(Q.front==Q.rear) return ERROR;
e =Q.front->next->data;
returnOK;
}
StatusEnQueue(LinkQueue&Q,QElemTypee){
//入队:插入元素e为Q的新的队尾元素
QueuePtrp;
p = (QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
*/
// ---基本操作的实现算法(函数)--------
StatusInitQueue(LinkQueue&Q){
//构造一个空队列
Q.front=Q.rear= (QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW); //存储分配失败
StatusEnQueue(LinkQueue&Q,QElemTypee)//入队:插入元素e为Q的新的队尾元素
StatusDeQueue(LinkQueue&Q,QElemType&e) //出队:若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
StatusQueueTraverse(LinkQueueQ, visit())//队列遍历:从队头到队尾依次对队列Q中的每个元素调用函数visit(),一旦visit失败,则操作失败。
}
StatusQueueEmpty(LinkQueueQ){
//若队列Q为空队列,则返回TRUE,否则返回FALSE
if(Q.front==Q.rear)return 1;
elsereturn 0;
}
intQueueLength(LinkQueueQ){
//返回Q的元素个数,即队列的长度
intn=0;QueuePtrp;
//元素访问函数
printf("%d ", e);
returnOK;
}
StatusQueueTraverse(LinkQueueQ, Status(* visit)(QElemType)){
//队列遍历:从队头到队尾依次对队列Q中的每个元素调用函数visit(),一旦visit失败,则操作失败。
QueuePtrp;
//将Q清为空队列
QueuePtrp;
p =Q.front;
Q.front=Q.front->next;
while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
Q.front= p;
Q.rear=Q.front;
returnOK;
StatusClearQueue(LinkQueue&Q)//将Q清为空队列
StatusQueueEmpty(LinkQueueQ)//若队列Q为空队列,则返回TRUE,否则返回iFALSE
intQueueLength(LinkQueueQ) //返回Q的元素个数,即队列的长度
StatusGetHead(LinkQueueQ,QElemType&e)//若队列不空,则用e返回Q的队头元素,并并返回OK;否则返回ERROR
/*
设计实现抽象数据类型“队列(Queue)”ADT定义见教材p59~60。
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedefintStatus;
}
//以下为队列应用程序段
if(DestroyQueue(Q) == OK)printf("已成功销毁队列!!!!");
returnOK;
}
p =Q.front->next;
while(p){
if(!visit(p->data)) return ERROR;
p = p->next;
}
returnOK;
}
intmain()
{
Status flag;inti, n;
QElemTypee;
LinkQueueQ;
flag=Initຫໍສະໝຸດ Baiduueue(Q);
p->data = e; p->next = NULL;
Q.rear->next = p;
Q.rear= p;
returnOK;
}
StatusDeQueue(LinkQueue&Q,QElemType&e){
//出队:若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR
QueuePtrp;
EnQueue(Q, e);
}
printf("\n目前队列长度为%d个元素!",QueueLength(Q));
QueueTraverse(Q, Print);
printf("\n出队结果为:\n");
while(!QueueEmpty(Q)){
DeQueue(Q, e);
printf("%d ", e);
QueuePtrfront; //队头指针
QueuePtrrear; //队尾指针
}LinkQueue;
/*
// ---基本操作的函数原型说明------(略)
StatusInitQueue(LinkQueue&Q)//构造一个空队列
StatusDestroyQueue(LinkQueue&Q)//销毁队列
相关文档
最新文档