1、实验三队列实验报告计算机科学与技术系实验报告专业名称 计算机科学与技术 课程名称 数据结构与算法 项目名称 实验三队列实验 班 级 学 号 姓 名 同组人员 无 实验日期 实验三队列实验实验题目:建立含有若干个元素的循环队列和链队列,并分别实现循环队列和 链队列的入队和出对操作。(1)先实现循环队列的入队和出队操作1问题分析本程序要求实现建立含有若干个元素的循环队列,并实现循环队列的入队和出队 操作。完成该实验需要以下4个子任务:1定义一个循环队列的存储结构,定义队列的基本算法。2定义一个display ()函数实现队列元素的输出看入队是否成功3通过队列的基本算法实现队列的出队操作在主函数中完
2、成操作 测试数据设计如下:1 2 3 4 562.概要设计为了实现上述程序功能,需要: 声明一个循环队列 定义出队列的基本算法,G通过键盘输入5个整数,入队,出队 在主函数中先往队列里输入 5 个元素,然后入队,输出,看入队是否成功,然后出队,再调用 display () 函数看是否出队。1)本程序包含7个函数:1主函数main()2.置空队:InitQueue()3.判对空:QueueEmpty()4.判队满:QueueFull()5.入队:Add()6.出队:Delete()7.display()各函数关系如下:Ini tQueue()QueueEmpty()Main ()QueueFul
3、l()Add()Ma inDelete()display()3、详细设计实现概要设计中定义的所有的数据类型,对每个操作给出了算法和代码,主 程序和模块都需要代码。(1)循环队列#defi ne maxle n 10typedef structint data maxle n;int front; in t rear;SeqQueue;(2)队列基本算法SeqQueue *ln itQueue(SeqQueue *q) / 建立一个空循环队列q=(SeqQueue *)malloc(sizeof (SeqQueue);q-fro nt=O;q-rear=0;return q;int QueueF
4、ull (SeqQueue *q) / 判断队列是否为满if (q-fron t=(q-rear+1)%maxle n)return 1;else return 0;int QueueEmpty(SeqQueue *q) / 判断队列是否为空if (q-rear=q-front)return 1;else return 0;void Add (SeqQueue *q,i nt x) /if(!QueueFull(q) q-rear=(q-rear+1)%maxle n; q-dataq-rear=x;else printf (queue full);void Delete(SeqQueue *
5、q) /if (!QueueEmpty(q) q-fron t=(q-fron t+1)%maxle n; else printf (queue Empty);(3)用display()函数输出循环队列元素void display(SeqQueue *q)int i;if(q-fron t!=q-rear)prin tf( 输出循环队列元素:);i=q-front;doi=(i+1)%maxle n; prin tf(%d,q-datai);while(i!=q-rear);elseprin tf( 队列为空!);(4)入队出队/输出循环队列q的元素/循环队列非空,输出队列元素在主函数中先往队
6、列里输入5个元素,输出,看入队是否成功,然后出队, 再调用display ()函数看是否出队。int main()SeqQueue *q;int i,y;q=I ni tQueue(q); /建立空循环队列printf(请输入5个元素);for(i=0;i5;i+)scan f(%d, &y); Add(q,y);display(q);for(i=0;i5;i+)Delete(q);/入队5个元素/输出循环队列元素/把5个元素出队printf(n请输入1个元素); sca nf(%d, &y);Add(q,y);display(q);return 0;/再入队一个元素/再输出循环队列元素4、调
7、试分析编译无错误5、用户使用说明程序名为class1.exe ,在DEBUGS件夹里面。运行环境 Visual C+ 6.0 。6、测试结果祷输入5个元亲1 2 3 4 5输出循环队列兀索:12315 请输Xi牛元拙陶出循坏队列元囁:6Press any key to continue7、附录#i nclude #i nclude #defi ne maxle n 10 typedef structint data maxle n;int front;int rear;SeqQueue;SeqQueue *ln itQueue(SeqQueue *q)q=(SeqQueue *)malloc(
8、sizeof (SeqQueue); q_fron t=0;q-rear=0;return q;/建立一个空循环队列int QueueFull (SeqQueue *q)if (q-fro nt=(q-rear+1)%maxle n) return 1;else return 0;int QueueEmpty(SeqQueue *q)if (q-rear=q-fro nt) return 1;else return 0;/判断队列是否为满/判断队列是否为空void Add (SeqQueue *q,i nt x)if(!QueueFull(q) q-rear=(q-rear+1)%maxle
9、n; q_dataq_rear=x;else printf (queue full);void Delete(SeqQueue *q)if (!QueueEmpty(q)q-fron t=(q- fron t+1)%maxle n; else printf (queue Empty);void display(SeqQueue *q)int i;if(q-fron t!=q-rear)printf(输出循环队列元素:i=q-front;/入队/出队/输出循环队列q的元素/循环队列非空,输出队列元素);doi=(i+1)%maxle n; prin tf(%d,q-datai);while(i!
10、=q-rear);elseprintf(”队列为空!);int mai n()SeqQueue *q;int i,y;q=ln itQueue(q);printf(请输入5个元素);for(i=0;i5;i+)scan f(%d, &y);Add(q,y);display(q);for(i=0;ifron t=(L in kList *)malloc(sizeof(L in kList);q-front-n ext=NULL;q-rear=q-fro nt;return q;int QueueEmpty(LinkQueue *q) / 判断队列是否为空if (q-rear=q-front)re
11、turn 1;else retur n 0;Lin kQueue *Add (Lin kQueue *q,i nt x) /Lin kList *p;p=(L in kList *)malloc(sizeof (Lin kList);p-data=x;p-n ext=NULL;q-rear- n ext=p;q-rear=p;return q;入队Lin kQueue *Delete(L in kQueue *q)Lin kList *p;if (!QueueEmpty(q)p=q-front-n ext;q-front-n ext=p-n ext;if(p-n ext=NULL)q-rear
12、=q-front; free(p);return q;else printf (queue Empty);(3)用display()函数输出链队列元素 void display(L in kQueue *q)Lin kList *p;p=q-front-n ext;if(p!=NULL)printf( 输出链队元素); while(p!=NULL)prin tf(%2d,p-data); p=p-n ext;else printf(n 链队为空);(4)在主函数中先往队列里输入 5个元素,再调用display ()函数看是否出队。/出队/输出链队列的元素输出,看入队是否成功,然后出队,int
13、main()int i,y=0;/建立空链队列/入队5个元素/把5个元素出队/看是否出队成功SetQueue();display(q);printf(n请输入5个元素);for(i=0;i5;i+)scan f(%d, &y);Add(q,y);display(q);for(i=0;i5;i+)Delete(q); display(q); return 0;4、调试分析编译无错误5、用户使用说明程序名为class12exe,在DEBUGS件夹里面。运行环境 Visual C+ 6.0 。6、测试结果陡陆为宁请输入3个元素1 2 3 4 5输出養艸元耒1 2 3 4 5竄仁为PP匚巳ss any
14、 key to cent inue7、附录#i nclude #i nclude #defi ne maxle n 10 typedef struct nodeint data;struct node *n ext;Lin kList;typedef structLin kList *fron t,*rear;Lin kQueue;Lin kQueue *q;Lin kQueue *SetQueue() / 建立一个空链队列q=(L in kQueue *)malloc(sizeof (Lin kQueue);q-fron t=(L in kList *)malloc(sizeof(L in
15、kList); q-front-n ext=NULL;q-rear=q-fro nt;return q;int QueueEmpty(LinkQueue *q) / 判断队列是否为空if (q-rear=q-fro nt)return 1;else return 0;Lin kQueue *Add (Lin kQueue *q,i nt x)Lin kList *p;p=(L in kList *)malloc(sizeof (Lin kList); p-data=x;p-n ext=NULL;q-rear- n ext=p;q-rear=p;return q;Lin kQueue *Dele
16、te(Li nkQueue *q)Lin kList *p;if (!QueueEmpty(q)p=q-front_n ext; q-front-n ext=p-n ext;if(p-n ext=NULL)q-rear=q-front; free(p);return q;else printf (queue Empty);/入队/出队/输出链队列的元素void display(L in kQueue *q)Lin kList *p;p=q-front_n ext; if(p!=NULL)printf(输出链队元素); while(p!=NULL)prin tf(%2d,p-data); p=p
17、-n ext;else printf(n 链队为空);int mai n()int i,y=0;SetQueue();display(q);printf(n请输入5个元素);for(i=0;i5;i+)scan f(%d, &y);Add(q,y);display(q);for(i=0;ifro nt=0;q-rear=0;return q;建立一个空循环队列int QueueFull (SeqQueue *q) /if (q-fron t=(q-rear+1)%maxle n) return 1;else return 0;判断队列是否为满int QueueEmpty(SeqQueue *q
18、) if (q-rear=q-front) return 1;else return 0;char GetHead(SeqQueue *q) if(!QueueEmpty(q)/判断队列是否为空/ 取栈顶元素retur n q-data(q-fr on t+1)%maxle n;else prin tf(queue Empty); void Add (SeqQueue *q,char x) /入队if(!QueueFull(q)q-rear=(q-rear+1)%maxle n; q-dataq-rear=x;else printf (queue full);出队void Delete(Seq
19、Queue *q) /if (!QueueEmpty(q)q-fron t=(q-fron t+1)%maxle n; else printf (queue Empty);(3)用print()函数验证程序void prin t(SeqQueue *q)char x,y,t;x=e;y=c;Add(q,h);Add(q,r);Add(q,y); x=GetHead(q);Delete(q);Add(q,x);x=GetHead(q);Delete(q);Add(q,a);while (QueueEmpty(q)=0) y=GetHead(q); Delete(q);prin tf(%c,y);
20、prin tf(%c,x);(4)在主函数中调用print ()函数验证程序int main()SeqQueue *q; q=ln itQueue(q); prin t(q);return 0;4、 调试分析编译无错误5、 用户使用说明程序名为class2.exe ,在DEBUGS件夹里面。运行环境 Visual C+ 6.0 。7、附录 #i nclude #i nclude #defi ne maxle n 10typedef structchar data maxle n;int front;int rear;SeqQueue;SeqQueue *lnitQueue(SeqQueue *
21、q) / 建立一个空循环队列q=(SeqQueue *)malloc(sizeof (SeqQueue); q_fron t=0;q-rear=0;return q;int QueueFull (SeqQueue *q)if (q-fro nt=(q-rear+1)%maxle n) return 1;else return 0;int QueueEmpty(SeqQueue *q) / 判断队列是否为空if (q-rear=q-fro nt)return 1;else return 0;char GetHead(SeqQueue *q) / 取栈顶元素if(!QueueEmpty(q)ret
22、ur n q-data(q-fr on t+1)%maxle n;else prin tf(queue Empty);void Add (SeqQueue *q,char x) / 入队if(!QueueFull(q)q-rear=(q-rear+1)%maxle n; q-dataq-rear=x;else printf (queue full);void Delete(SeqQueue *q) / 出队if (!QueueEmpty(q)q-fron t=(q- fron t+1)%maxle n;else printf (queue Empty);void prin t(SeqQueue *q)char x,y,t;x=e;y=c;Add(q,h);Add(q,r);Add(q,y); x=GetHead(q);Delete(q);Add(q,x);x=GetHead(q);Delete(q);Add(q,a);while (QueueEmpty(q)=0)y=GetHead(q);Delete(q);prin tf(%
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1