实验三队列实验报告docxWord格式文档下载.docx
《实验三队列实验报告docxWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验三队列实验报告docxWord格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。
4.判队满:
QUeUeFUll()
5.入队:
AddO
6.出队:
DeIeteO
7.display()
各函数关系如下:
InitQueue()
QueueEmptyO
QueueFuI1()
Add()Main
DeIete()display()
3、详细设计
实现概要设计中定义的所有的数据类型,对每个操作给出了算法和代码,主程序和模块都需要代码。
(1)循环队列
#definemaxlen10
typedefStrUCt{
intdata[maxlen];
intfront;
intrear;
}SeqQueue;
(2)队列基本算法
SeqQUeUe*lnitQueue(SeqQueue*q)//建立一个空循环队列
{
q=(SeqQueue*)malloc(sizeof(SeqQueue));
q->
front=0;
rear=0;
returnq;
}
intQueueFull(SeqQUeUe*q){//判断队列是否为满
if(q->
front==(q->
rear+1)%maxlen)
return1;
elsereturn0;
intQueueEmpty(SeqQueue*q){
rear==q->
front)
voidAdd(SeqQueue*q,intx)//
if(!
QueueFull(q)){
rear=(q->
rear+1)%maxlen;
q->
data[q->
rear]=x;
}elseprintf("
queuefull"
);
voidDelete(SeqQueue*q){//
if(!
QueueEmpty(q))q->
front=(q->
front+1)%maxlen;
elseprintf("
queueEmpty"
(3)用display()函数输出循环队列元素
voiddisplay(SeqQueue*q)
inti;
if(q->
front!
=q->
rear)
printf("
输出循环队列元素:
"
i=q->
front;
do
i=(i+1)%maxlen;
%d"
q->
data[i]);
}while(i!
rear);
else
队列为空!
//判断队列是否为空
入队
出队
//输出循环队列q的元素
//循环队列非空,输出队列元素
(4)在主函数中先往队列里输入5个元素,输出,看入队是否成功,然后出队,再调用display()函数看是否出队。
intmain()
SeqQueue*q;
//建立空循环队列
//入队5个元素
//输出循环队列元素
//把5个元素出队
//再入队一个元素
//再输出循环队列元素
inti,y;
q=lnitQueue(q);
Printf("
请输入5个元素"
for(i=0;
i<
5;
i++)
SCanf("
&
y);
Add(q,y);
}display(q);
for(i=0;
DeIete(q);
\n请输入1个元素"
SCanf("
display(q);
return0;
4、调试分析
编译无错误
5、用户使用说明
程序名为class1.exe,在DEBUGt件夹里面。
运行环境ViSUaIC++6.0。
6、测试结果
请输入吕个元素12345
⅛i由循环罠列元素:
12345
请⅛λl÷
7E⅛6
输出循环队列元索:
6PressanykeytoCOntinUe
7、附录
#include<
stdio.h>
#include<
stdlib.h>
typedefstruct{
intdata[maxlen];
intrear;
SeqQueue*InitQueue(SeqQueue*q)//建立一个空循环队列{
front=0;
intQueueFull(SeqQueue*q){//判断队列是否为满
front==(q->
rear+1)%maxlen)return1;
voidAdd(SeqQueue*q,intx)
QueueFull(q)){q->
voidDelete(SeqQueue*q){
//入队
//出队
//输出循环队列q的元素
//循环队列非空,输出队列元素
i=q->
printf("
}while(i!
q=InitQueue(q);
请输入5个元素"
scanf("
&
Delete(q);
\n请输入1个元素"
scanf("
display(q);
//建立空循环队列
//入队5个元素
//输出循环队列元素
//把5个元素出队
//再入队一个元素
//再输出循环队列元素
(2)实现链队列的入队和出队操作
1.问题分析
本程序要求实现建立含有若干个元素的链队列,并实现链队列入队和出队操作
完成该实验需要以下4个子任务:
(1定义一个链队列的存储结构,定义链队列的基本算法。
14在主函数中完成操作
为了实现上述程序功能,需要:
11声明一个链队列12定义出链队列的基本算法,
13通过键盘输入5个整数,入队,出队14在主函数中先往队列里输入5个元素,然后入队,输出,看入队是否成功,然后出队,再调用display()函数看是否出队。
2)本程序包含6个函数:
1主函数main()
InitQueue()
3.判对空:
QueueEmpty()
4.入队:
Addo
5.出队:
DeIeteo
6.display()
SetQueue()
Add()
DeIete()
display()
4、详细设计
(1)链队列
typedefStrUCtnode{
intdata;
StrUCtnode*next;
}LinkList;
LinkList*front,*rear;
}LinkQueue;
LinkQueue*q;
LinkQueue*SetQueue()//建立一个空链队列
q=(LinkQueue*)malloc(sizeof(LinkQueue));
front=(LinkList*)malloc(sizeof(LinkList));
front->
next=NULL;
rear=q->
front;
intQueueEmpty(LinkQueue*q){if(q->
LinkQueue*Add(LinkQueue*q,intx)
LinkList*p;
p=(LinkList*)malloc(sizeof(LinkList));
p->
data=x;
next=NULL;
rear->
next=p;
rear=p;
LinkQueue*Delete(LinkQueue*q){
QueueEmpty(q)){p=q->
next;
next=p->
if(p->
next=NULL)q->
free(p);
(3)用display()函数输出链队列元素voiddisplay(LinkQueue*q)
p=q->
if(p!
=NULL)
输出链队元素"
while(p!
{printf("
%2d"
p->
data);
p=p->
elseprintf("
\n链队为空"
//输出链队列的元素
然后出队,
//建立空链队列
//看是否出队成功
(4)在主函数中先往队列里输入5个元素,输出,看入队是否成功,再调用display()函数看是否出队。
inti,y=O;
SetQueue();
display(q);
∖n请输入5个元素"
return0;
4、调试分析
5、用户使用说明
程序名为class12exe,在DEBUGt件夹里面。
6、测试结果
链队为穹
诸输入5个元索12345
输陆元壷12345
麓队为⅞PressanykeytoCOntinlle
#include<
#definemaxlen10typedefstructnode{intdata;
structnode*next;
}LinkList;
LinkList*front,*rear;
}LinkQueue;
LinkQueue*q;
LinkQueue*SetQueue()//建立一个空链队列
{q=(LinkQueue*)malloc(sizeof(LinkQueue));
front=(LinkList*)malloc(sizeof(LinkList));
returnq;
intQueueEmpty(LinkQueue*q){//判断队列是否为空if(q->
LinkQueue*Add(LinkQueue*q,intx)//入队
p=(LinkList*)malloc(sizeof(LinkList));
p->
LinkQueue*Delete(LinkQueue*q){//出队
voiddisplay(LinkQueue*q)
while(p!
inti,y=0;
display(q);
\n请输入5个元素"
//建立链队列
//看是否出队成功
实验题目2:
写出下列程序段的输出结果,并通过程序运行来验证1•问题分析
(£
定义一个循环队列的存储结构,定义队列的基本算法。
②定义一个Print()函数实现程序
②在主函数调用print()函数看是否出队。
3)本程序包含8个函数:
8.GetHeado
各函数关系如下:
GetHead()
Print()
5、详细设计
Chardata[maxlen];
SeqQUeUe*lnitQueue(SeqQueue*q)//建立一个空循环队列
intQUeUeEmPty(SeqQUeUe*q){//判断队列是否为空
charGetHead(SeqQueue*q){//
QueueEmpty(q))
returnq->
data[(q->
front+1)%maxlen];
voidAdd(SeqQueue*q,charx)//
(3)用print()函数验证程序
voidprint(SeqQueue*q)
charx,y,t;
x='
e'
;
y='
c'
Add(q,'
h'
r'
x=GetHead(q);
Add(q,x);
a'
while(QueueEmpty(q)==0){y=GetHead(q);
Delete(q);
%c"
y);
x);
取栈顶元素
(4)在主函数中调用Print()函数验证程序
q=lnitQueue(q);
Print(q);
程序名为CIaSS2.exe,在DEBUGt件夹里面。
I■ILZ・VL∙∣JV^-3i2L⅜⅝-∣U£
_XLZJkJU弓⅜⅝uIlJIJ4£
_・JKJ
charPressanykeytoCOntinue
#includeVStdiO.h>
#includeVStdIib.h>
#definemaxlen10typedefStrUCt{
Chardata[maxlen];
//建立一个空循环队列
SeqQUeUe*lnitQueue(SeqQueue*q)
intQueueFull(SeqQueue*q){
//判断队列是否为满
intQueueEmpty(SeqQueue*q){if(q->
front)return1;
charGetHead(SeqQueue*q){
//取栈顶元素
elseprintf("
voidAdd(SeqQueue*q,charx)//入队{
voidDelete(SeqQueue*q){//出队if(!
voidprint(SeqQueue*q){
x='
Add(q,'
int