实验名称:实验四队列的基本操作实验目的掌握队列这种抽象数据类型的特点及实现方法。
实验内容从键盘读入若干个整数,建一个顺序队列或链式队列,并完成下列操作:(1)初始化队列;(2)队列是否为空;(3)出队;(4)入队。
算法设计分析(一)数据结构的定义单链表存储结构定义为:struct Node; //链表单链表typedef struct Node *PNode;int dui;dui =1;struct Node{int info;PNode link;};struct LinkQueue{PNode f;PNode r;};typedef struct LinkQueue *PLinkQueue;(二)总体设计程序由主函数、创建队列函数、判断是否为空队列函数、入队函数、出队函数、取数函数、显示队列函数、菜单函数组成。
其功能描述如下:(1)主函数:调用各个函数以实现相应功能main(){PLinkQueue a; //定义链表aint b,c,e; //b 菜单选择c选择继续输入e输入元素do{//菜单选择mune();scanf("%d",&b);switch(b){case 1://初始化a=create(); //初始化队列case 2: //入队do{printf("\n请输入需要入队的数:");if(e!=NULL){scanf("%d",&e);enQueue(a,e);}printf("是否继续入队?(是:1 否:0)\n");scanf("%d",&c);}while(c==1);break;case 3: //出队c=frontQueue(a);deQueue(a);if(dui!=0){printf("\n出队为:%d\n",c);}dui=1;break;case 4: //显示队中元素showQueue(a);break;case 5:return;default:printf("输入错误,程序结束!\n");return;}}while(a!=5);{return 0;}}(三)各函数的详细设计:Function1:PLinkQueue create(void)//创队{PLinkQueue plqu;plqu=(PLinkQueue)malloc(sizeof(struct LinkQueue));if(plqu!=NULL){plqu->f=NULL;plqu->r=NULL;printf("初始化成功!");}elseprintf("初始化失败!");return plqu;}Function2:int isEmpty(PLinkQueue plqu)//判断是否为空{return(plqu->f==NULL);}Function3:void enQueue(PLinkQueue plqu,int x)//入队{PNode p;p=(PNode)malloc(sizeof(struct Node));if(p==NULL)printf("入队失败,请重新入队!");else{p->info=x;p->link=NULL;if(plqu->f==NULL)plqu->f=p;elseplqu->r->link=p;plqu->r=p;}}Function4:void enQueue(PLinkQueue plqu,int x)//入队{PNode p;p=(PNode)malloc(sizeof(struct Node));if(p==NULL)printf("入队失败,请重新入队!");else{p->info=x;p->link=NULL;if(plqu->f==NULL)plqu->f=p;elseplqu->r->link=p;plqu->r=p;}}Fubction5:void deQueue(PLinkQueue plqu)//出队{PNode p;if(plqu->f==NULL)printf("队已经空了!\n");else{p=plqu->f;plqu->f=p->link;free(p);}}Function6:int frontQueue(PLinkQueue plqu)//取数{if(plqu->f==NULL){dui=0;return 0;}else{return(plqu->f->info);}}Function7:void showQueue(PLinkQueue plqu)//显示队中的数输出队列{PNode p;p=plqu->f;printf("队列中的数:");if(plqu->f==NULL)printf("队是空的!\n");else{while(p->link!=NULL){printf("%d",p->info);p=p->link;}printf("%d\n",plqu->r->info);}}Function8:void mune(){printf("--------------------------------");printf("\n\t请选择队的相关功能\n");printf("\t1.初始化队列\n");printf("\t2.入队\n");printf("\t3.出队\n");printf("\t4.显示队中的元素\n");printf("\t5.退出\n");}实验测试结果及结果分析(一)测试结果(二)结果分析(1)运行程序依次输入入队1-6,并且显示入队元素,队列有1 2 3 4 5 6 ,说明入队成功。
(2)出队一个元素,数字1成功出队,并显示队中元素。
队列中没有1说明出队成功。
实验总结队列时一种先进先出的线性表,只允许在表的一端进行插入,而在另一端删除元素。
在本次实验中通过对对队列的链式表示与实现,加深了对链队列的特点的理解。
虽然在实验中遇到一些调试问题,但经过分析最终达到了预期的效果。
附录实验程序代码#include<stdio.h>#include <stdlib.h>struct Node; //链表单链表typedef struct Node *PNode;int dui;dui =1;struct Node{int info;PNode link;};struct LinkQueue{PNode f;PNode r;};typedef struct LinkQueue *PLinkQueue;PLinkQueue create(void)//创队{PLinkQueue plqu;plqu=(PLinkQueue)malloc(sizeof(struct LinkQueue));if(plqu!=NULL){plqu->f=NULL;plqu->r=NULL;printf("初始化成功!");}elseprintf("初始化失败!");return plqu;}int isEmpty(PLinkQueue plqu)//判断是否为空{return(plqu->f==NULL);}void enQueue(PLinkQueue plqu,int x)//入队{PNode p;p=(PNode)malloc(sizeof(struct Node));if(p==NULL)printf("入队失败,请重新入队!");else{p->info=x;p->link=NULL;if(plqu->f==NULL)plqu->f=p;elseplqu->r->link=p;plqu->r=p;}}void deQueue(PLinkQueue plqu)//出队{PNode p;if(plqu->f==NULL)printf("队已经空了!\n");else{p=plqu->f;plqu->f=p->link;free(p);}}int frontQueue(PLinkQueue plqu)//取数{if(plqu->f==NULL){dui=0;return 0;}else{return(plqu->f->info);}}void showQueue(PLinkQueue plqu)//显示队中的数输出队列{PNode p;p=plqu->f;printf("队列中的数:");if(plqu->f==NULL)printf("队是空的!\n");else{while(p->link!=NULL){printf("%d",p->info);p=p->link;}printf("%d\n",plqu->r->info);}}void mune(){printf("--------------------------------");printf("\n\t请选择队的相关功能\n");printf("\t1.初始化队列\n");printf("\t2.入队\n");printf("\t3.出队\n");printf("\t4.显示队中的元素\n");printf("\t5.退出\n");}main(){PLinkQueue a; //定义链表aint b,c,e; //b 菜单选择c选择继续输入e输入元素do{//菜单选择mune();scanf("%d",&b);switch(b){case 1://初始化a=create(); //初始化队列case 2: //入队do{printf("\n请输入需要入队的数:");if(e!=NULL){scanf("%d",&e);enQueue(a,e);}printf("是否继续入队?(是:1 否:0)\n");scanf("%d",&c);}while(c==1);break;case 3: //出队c=frontQueue(a);deQueue(a);if(dui!=0){printf("\n出队为:%d\n",c);}dui=1;break;case 4: //显示队中元素showQueue(a);break;case 5:return;default:printf("输入错误,程序结束!\n");return;}}while(a!=5);{return 0;}}。