文档详情

实验302 队列的链式表示和实现(lnkqueue).doc

发布:2017-12-18约5.06千字共5页下载文档
文本预览下载声明
实验302 队列的链式表示和实现 编写一个程序实现链队列的各种基本运算,并在此基础上设计一个主程序,完成如下功能: 初始化并建立链队列。 入链队列。 出链队列。 遍历链队列。 【参考程序清单】 # include stdio.h # include stdlib.h #defind Elemtype int typedef struct Qnode { Elemtype data; struct Qnode * next; }Qnodetype; typedef struct { Qnodetype *front; Qnodetype *rear; }Lqueue; void Lappend(Lqueue *q,int x); /*初始化并建立链队列*/ void creat(Lqueue *q) { Qnodetype *h; int i,n,x; printf(输入将建立连队列元素的个数:n=); scanf(%d,n); h=(Qnodetype *)malloc(sizeof(Qnodetype )); h-next=NULL q-front=h; q-rear=h; for(i=1;i,=n;i++) { printf(链队列第%d个元素的值为:,i); scanf(%d,x); Lappend(q,x); } } /*如链队列*/ void Lappend(Lqueue *q,int x) { Qnodetype *s; s=(Qnodetype *)malloc(sizeof(Qnodetype)); s-data=x; s-next=NULL; q-rear-next=s; q-rear=s; } /*出链队列*/ Elemtype Ldelete(Lqueue *q) { Qnodetype *p Elemtype x; if(q-front==q-rear) { printf(队列为空! \n); return 0; } else { p=q-front-next; q-front-next=p-next; if(p-next==NULL) q-rear=q-front; x=p-data; free(p); } printf(出链队列元素: %d\n,x);return(x); } /*遍历链队列*/ void display(Lqueue *q) { Qnodetype *p; p=q-front-next; /*指向第一个数据元素结点*/ if(! p) printf(队列为空! \n); else { printf(\nl链队列元素依然为:); while(p!=NULL) { printf(%d--,p-data); p=p-next; } printf(\n\n遍历链队列结束!\n); } } void main() { Lqueue *p; int x,cord; printf(\n******第一次操作请选择初始化并建立链队列! ******\n); do { printf(=============主菜单==========\n); printf( 1 初始化并建立链队列); printf( 2 入列队列 \n); printf( 3 出链队列 \n); printf( 4 遍历链队列 \n ); printf( 5 结束程序运行 \n ); printf( ======================= \n ); scanf(%d,cord); switch(cord) { case
显示全部
相似文档