欢迎来到天天文库
浏览记录
ID:13740279
大小:47.00 KB
页数:3页
时间:2018-07-24
《实验6、图的邻接表的建立》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验六、图的邻接表的建立一、实验目的:1.理解图的数据结构。2.掌握图的邻接表的存储结构,建立邻接表的算法。二、实验步骤:1.读懂实验原理,参照参考程序编写上机程序并运行、调试。2.按照图输入数据验证结果。三、参考程序:#include#include#definevtxnum5#definenull0structarcnode{intadjvex;structarcnode*next;};structvexnode{intdata;structarcnode*firstare;};voidmain(){inti,j,flag;intch;str
2、uctarcnode*p,*s,*z;structvexnodea[6];for(i=1;i<=vtxnum;i++){flag=0;a[i].firstare=null;a[i].data=i;do{printf("图中%d结点有结点与其相关联吗?[有关联请输入0,无关联请输入1]:",i);scanf("%d",&ch);if(ch==0){printf("输入与%d结点相关联的结点,输入零结束",i);scanf("%d",&j);p=(structarcnode*)malloc(sizeof(structarcnode));a[i].firstare=p;s=p;z=s;wh
3、ile(j!=0){flag=flag+1;while((j<0)
4、
5、(j>vtxnum)){printf("您输入的结点超出规定范围,重新输入与%d结点相关的结点",i);scanf("%d",&j);}if(j!=0){z=s;s->adjvex=j;p=(structarcnode*)malloc(sizeof(structarcnode));s->next=p;s=p;}else{if(flag==1)a[i].firstare=null;elsez->next=null;}printf("输入与%d结点相关联的结点,键入零结束",i);scanf("%d",&j);}z->
6、next=null;}else{if(ch==1)a[i].firstare=null;elseprintf("重新输入");}}while((ch!=1)&&(ch!=0));}for(i=1;i<=vtxnum;i++){p=a[i].firstare;if(p==null)printf("%d结点是一个弧零结点",i);else{printf("%d",i);while(p){printf("----->");printf("%d",p->adjvex);p=p->next;}}printf("");}}四、输出结果:
此文档下载收益归作者所有