数据结构实验报告队列的表示与实现Word文档下载推荐.docx
《数据结构实验报告队列的表示与实现Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告队列的表示与实现Word文档下载推荐.docx(19页珍藏版)》请在冰豆网上搜索。
插入元素e为Q的新的队尾元素
DeQueue(&Q,&e)//初始条件:
删除Q的队头元素,并用e返回其值
QueueTraverse(Q,visit())//初始条件:
Q已存在且非空
从队头到队尾,依次对Q的每个数据元素调用函数visit()。
一旦visit()失败,则操作失败。
}ADTQueue
与线性表类似,队列有两种存储表示链队列和循环队列。
//-——--基本操作的函数原型说明—-—-—
statusINitQueue(LinkQueue&
Q)//构造一个空队列Q
StatusDestoryQueue(LinkQueue&
Q)//销毁队列Q,Q不再存在
StatusClearQueue(LinkQueue&
Q)//将Q清为空队列
StatusQueueEmpty(LinkQueueQ)
//若队列Q为空队列,则返回TRUE,否则返回FALSE
intQueueLength(LinkQueueQ)//返回Q的元素个数,即为队列的长度
StatusGetHead(LinkQueueQ,QElemType&
e)
//若队列不空,则用e返回Q的队头元素,并返回OK;
否则返回ERROR
StatusENQueue(LinkQueue&Q,QElemTypee)//插入元素e为Q的新的队尾元素
StatusDeQueue(LinkQueue&Q,QElemType&e)
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
statusQueueTraverse(linkQueueQ,visit())
//从队头到队尾依次对队列Q中的每个元素调用函数visit()。
一旦visit失败,则操作失败。
链队列:
//单链队列--队列的链式存储结构
typedefstructQNode{
QElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct{
QueuePtrfront;
//队头指针
QueuePtrrear;
//队尾指针
}LinkQueue;
//—-—-—单链队列的基本操作的算法描述——————
Q){//构造一个空队列Q
Q。
front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!
Q。
front)exit(OVERFLOW);
//存储分配失败
front—>
next=NULL;
returnOK;
}
Q){//销毁队列Q,Q不再存在
while(Q。
front){
Q.rear=Q.front->
next;
free(Q。
front);
Q。
front=Q。
rear;
}
returnOK;
Q)
//将Q清为空队列
//若队列Q为空队列,则返回TRUE,否则返回FALSE
intQueueLength(LinkQueueQ)
//返回Q的元素个数,即为队列的长度
StatusGetHead(LinkQueueQ,QElemType&e)
StatusENQueue(LinkQueue&
Q,QElemTypee){//插入元素e为Q的新的队尾元素
p=(QueuePtr)malloc(sizeof(QNode));
if(!
p)exit(OVERFLOW);
p—>
data=e;
p—〉next=NULL;
rear->
next=p;
Q.rear=p;
StatusDeQueue(LinkQueue&Q,QElemType&e){
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
if(Q。
front==Q。
rear)returnERROR;
p=Q.front—〉next;
e=p—〉data;
Q.front->
next=p->
next;
if(Q.rear==p)Q。
rear=Q.front;
free(p);
statusQueueTraverse(linkQueueQ,visit())
//一旦visit失败,则操作失败。
循环队列:
//循环队列——队列的顺序存储结构
#defineMAXQSIZE100//最大队列长度
QElemType*base;
//初始化的动态分配存储空间
intfront;
//头指针,若队列不空,指向队列头元素
intrear;
//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
//-————循环队列的基本操作的算法描述—--—-—
StatusInitQueue(SqQueue&
Q){
//构造一个空队列Q
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
base)exit(OVERFLOW);
//存储分配失败
Q.front=Q。
rear=0;
intQueueLength(SqQueueQ){//返回Q的元素个数,即队列的长度
return(Q。
rear-Q.front+MAXQSIZE)%MAXQSIZE;
statusEnQueue(SqQueue&
Q,QElemType){//插入元素e为Q的新的队尾元素
if((Q。
rear+1)%MAXQSIZE==Q。
front)returnERROR;
//队列满
Q.base[Q.rear]=e;
rear=(Q.rear+1)%MAXQSIZE;
StatusDeQueue(Squeue&
Q,QElemType&e){
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
//否则返回ERROR
front==Q.rear)returnERROR;
e=Q。
base[Q.front];
front=(Q.front+1)%MAXQSIZE;
3、事先定义的常量和类型
/***********************C函数库定义****************/
#include<
stdio.h〉//EOFNULL
#include<
string。
h>
malloc.h〉//malloc();
limits.h〉//ENTMAX
stdlib.h>
math。
//floor(),fabs(),abs(),ceil(),
io。
h〉//eof()
#include〈process。
h〉//exit()
#include〈iostream。
//cout,cin
/******************函数结果状态代码****************/
#defineTRUE1
#defineFALSE0
#defineOK1
#defineERROR0
#defineINFEASIBLE-1
//#defineOVERFLOW—2//因为math。
h中已经定义OVERFLOW为3
typedefintQElemType;
//ElemType是变量的类型定义
typedefintStatus;
//Status是函数的类型,其值是函数结果状态代码,如OK,TRUE。
4、实验过程
链队列
#include<
stdio。
h〉
stdlib。
#include〈process。
#defineOK1
#defineOVERFLOW0
typedefstructQNode
{
intdata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{
QueuePtrrear;
}LinkQueue;
intInitQueue(LinkQueue&
rear=Q.front=(QueuePtr)malloc(sizeof(QNode));
Q.rear)
exit(OVERFLOW);
next=NULL;
voidQueueEmpty(LinkQueueQ)
if(Q.front==Q.rear)
printf(”该链队为空:
”);
else
printf("
该链队不为空:
"
);
voidEnQueue(LinkQueue&
Q,inte)
QueuePtrp;
p=(QueuePtr)malloc(sizeof(QNode));
p)
printf(”error”);
p-〉data=e;
Q.rear-〉next=p;
Q.rear=p;
printf(”元素%d入队成功"
e);
intEnnQueue(LinkQueue&
Q,inte)
returnERROR;
rear-〉next=p;
voidDeQueue(LinkQueue&
QueuePtrp;
该链队为空”);
p=Q.front—>
front—〉next=p->
rear==p)
rear=Q.front;
printf(”队首元素删除成功"
voidGetHead(LinkQueue&Q)
rear)
该链队为空"
p=Q。
front—>
printf("
队首元素为:
%d”,p-〉data);
voidOutQueue(LinkQueue&Q)
if(Q.front==Q。
);
front-〉next;
while(p!
=Q。
rear->
next)
{
%d"
p->
data);
p=p-〉next;
}
voidLengthQueue(LinkQueue&Q)
intf=0;
该队列的长度是:
,f);
p=Q.front—〉next;
while(p!
=Q.rear—〉next)
{
p=p-〉next;
f++;
}
%d”,f);
}
voidmain()
system(”cls"
intflag=1,i;
LinkQueueQ;
InitQueue(Q);
printf(”************************链队列功能菜单***********************\n"
printf(”1:
初始化链队列,2:
判断链队列是否为空,3:
进入队列,4:
取出队首元素\n”);
printf(”5:
输出该队列的所有元素,6:
输出该队列的长度,7:
结束程序,8:
清屏\n”);
while(flag)
{
printf(”\n请输入操作符:
”);
scanf("
%d"
,&i);
switch(i)
case1:
inte,n,k;
printf("
请输入队列的长度:
scanf("
%d”,&n);
请输入队列的元素:
for(e=1;
e〈=n;
e++)
{
scanf("
%d”,&k);
EnnQueue(Q,k);
}
printf(”初始化链队成功”);
break;
case2:
QueueEmpty(Q);
case3:
intj;
printf(”请输入要进入队列的元素”);
scanf(”%d"
,&j);
EnQueue(Q,j);
break;
case4:
GetHead(Q);
case5:
printf(”该队列的元素是:
OutQueue(Q);
case6:
LengthQueue(Q);
case7:
flag=0;
case8:
system(”cls”);
}
printf(”程序结束"
循环队列
#include〈stdio。
#include〈process。
#defineMAXSIZE10;
#defineOK1;
#defineERROR0;
#defineOVERFLOW0;
typedefstruct
int*data;
intfront;
intrear;
}SqQueue;
intInitQueue_Sq(SqQueue&Q)
data=(int*)malloc(10*sizeof(int));
Q.data)
exit(0);
Q.front=Q.rear=0;
intEnQueue_Sq(SqQueue&
rear+1)%10==Q.front)
returnERROR;
Q.data[Q。
rear]=e;
Q.rear=(Q.rear+1)%10;
voidIfEmpty(SqQueueQ)
if(Q.rear=Q.front)
printf(”该循环队列是空队列\n"
printf(”该循环队列不是空队列\n”);
voidIfFull(SqQueueQ)
if((Q.rear+1)%10==Q.front)
printf(”该循环队列已满\n”);
该循环队列未满\n”);
voidInQueue_Sq(SqQueue&
rear+1)%10==Q.front)
循环队列已满\n"
data[Q。
rear]=e;
Q.rear=(Q.rear+1)%10;
元素%d成功进入循环队列\n”,e);
voidDeQueue_Sq(SqQueue&Q)
inte;
循环队列为空\n"
e=Q.data[Q。
front];
printf(”循环队列队首元素是:
%d\n”,e);
voidDE_Sq(SqQueue&Q)
int*w;
w=&Q.data[Q。
front];
front=Q。
front+1;
队首元数%d删除成功\n”,*w);
intLength_Sq(SqQueue&Q)
ints;
s=(Q。
rear—Q.front+10);
returns%10;
intOutQueue_Sq(SqQueueQ)
SqQueuep;
p=Q;
inti,n;
n=Length_Sq(p);
for(i=0;
i〈n;
i++)
printf(”%d"
,p。
data[p。
front]);
p.front++;
voidDelet(SqQueue&Q)
free(Q。
printf(”释放成功"
system(”cls”);
printf(”**********************循环队列功能菜单***********************\n”);
1.初始化队列输入的数不超过10个,2。
判断队列是否空,3。
判断队列是否满,\n"
printf(”4.将元素入队,5。
取队列首元素,6。
队列的长度,7。
遍历循环队列,\n"
printf(”8.删除队首元素,9.释放队列,10。
清屏,0。
结束程序,\n"
intflag=1,i;
SqQueueQ;
InitQueue_Sq(Q);
while(flag)
printf(”请输入操作符:
scanf(”%d”,&i);
{
case1:
intn,j,m;
请输入初始化元素的个数:
%d”,&
n);
printf(”请输入元素:
for(j=0;
j〈n;
j++)
{
,&
m);
EnQueue_Sq(Q,m);
}
IfEmpty(Q);
case3:
IfFull(Q);
intk;
请输入要进入循环队列的元素:
k);
InQueue_Sq(Q,k);
DeQueue_Sq(Q);
intf;
f=Length_Sq(Q);
该循环队列的长度为:
%d\n"
f);
printf(”该循环队列为:
OutQueue_Sq(Q);
printf(”\n”);
case8:
DE_Sq(Q);
case9:
Delet(Q);
case10:
system("
cls”);
case0:
程序结束"
4、实验总结
队列是一种先进先出的线性表,它只允许在表的一端进行插入,而在另一端删除元素,在C语言中不能用动态分配的一维数组来实现循环队列,如果用户的应用程序中设有循环队列,则必须为它设定一个最大队列长度;
若用户无法预估所用队列的最大长度,则宜采用链队列。
本次实验通过对队列的链式表示与实现、队列的顺序表示与实现,加深了对链队列和循环队列的特点的理解,并且熟悉了VC6.0集成环境,虽然在调试过程中遇到了一些问题,但经分析后达到了预期的结果。