| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1283 人关注过本帖
标题:无向图的多重邻接表
只看楼主 加入收藏
liweiqing
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2007-10-16
收藏
 问题点数:0 回复次数:4 
无向图的多重邻接表
跪求:::无向图的多重邻接表!!!!!
2007-12-13 22:42
liweiqing
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2007-10-16
收藏
得分:0 

岂能尽如人意,但求无愧于心.
2007-12-17 18:24
liweiqing
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2007-10-16
收藏
得分:0 
哎!!!

岂能尽如人意,但求无愧于心.
2007-12-17 18:26
kelifei
Rank: 1
来 自:UESTC
等 级:新手上路
帖 子:89
专家分:0
注 册:2006-5-11
收藏
得分:0 
题目:编写一个算法由依次输入的顶点数目,边的数目,各顶点的信息和各条边的信息建立无向图的邻接多重表。

一.   需求分析

  这里需要两个主要字函数,一个是建立图,另一个是打印图。

二.   概要设计

首先是建立两个结点,一个是边结点,另一个是顶点结点,分别为struct Edge,struct Node,然后建立图,Create_ML_Graph(int Vertex1,NextEdge New),紧接着是打印Print_ML_Graph(struct Node *Head)。

三.   详细设计

   

#include <stdlib.h>

#include <stdio.h>

#define VertexNum  6

#define NULL ((void *)0)

struct Edge

{int Marked;

 int Vertex1;

 int Vertex2;

 struct Edge *Edge1;

 struct Edge *Edge2;

};

 typedef struct Edge *NextEdge;

 struct Node

{int Vertex;

 struct Edge *Next;

};

 typedef struct Node *Graph;

 struct Node Head[VertexNum];

 void Create_ML_Graph(int Vertex1,NextEdge New)

{NextEdge Pointer;

 NextEdge Previous;

 Previous=NULL;

 Pointer=Head[Vertex1].Next;

 while(Pointer!=NULL)

{Previous=Pointer;

 if (Pointer->Vertex1==Vertex1)

 Pointer=Pointer->Edge1;

 else  Pointer=Pointer->Edge2;

 }

 if(Previous==NULL)

 Head[Vertex1].Next=New;

 else if(Previous->Vertex1==Vertex1)

 Previous->Edge1=New;

 else Previous->Edge2=New;

 }

 void Print_ML_Graph(struct Node *Head)

{NextEdge Pointer;

 Pointer=Head->Next;

 while(    Pointer!=NULL)

{printf("(%d,%d)",Pointer->Vertex1,Pointer->Vertex2);

 if(Head->Vertex==Pointer->Vertex1)

 Pointer=Pointer->Edge1;

 else if(Head->Vertex==Pointer->Vertex2)

 Pointer=Pointer->Edge2;

 }

 printf("\n");

}

void main()

{int Source;

 int Destinition;

 int Choose;

 NextEdge New;

 int i;

 for(i=0;i<VertexNum;i++)

 {Head[i].Vertex=i;

  Head[i].Next=NULL;}

  printf("1.Undirected Graph\n");

  printf("2.Directed Graph\n");

  printf("Please choose:");

  scanf("%d",&Choose);

  while(1)

 {printf("Please input the Edge's source:");

 scanf("%d",&Source);

 if(Source==-1) break;

 printf("Please input the Edge's Destinition:");

 scanf("%d",&Destinition);

 if(Source>=VertexNum||Destinition>=VertexNum)

 printf("@Error@:out of range!!\n");

 else

 { New=(NextEdge) malloc(sizeof(struct Edge));

  if(New!=NULL)

 {New->Vertex1=Source;

  New->Vertex2=Destinition;

  New->Edge1=NULL;

  New->Edge2=NULL;

  Create_ML_Graph(Destinition,New);}

  }

 }

 printf("##Graph##\n");

 for(i=0;i<VertexNum;i++)

 {printf("Vertex[%d]:",i);

  Print_ML_Graph(&Head[i]);

  }

 }

四.   调试分析

  这个题在调试时,除了常规的变量的定义和指针等错误外,主要是指针的值传不过去,导致打印的时候输入的图打印不出来,检查的时候看各指针是不是传过去了(用单步执行)。

五.   用户使用说明

  运行程序时,首先是让你选择这时你输入1回车,这时让你输入头结点数,你可以输入1或2等(但不能大于6,这里设的最大值是6),紧接着让你输入尾结点,你照样输入(不能大于6),这样反复输入几次也就是几条边后回车,就可以看结果。

六.   测试结果

  依次输入1,2,1,3,2,4,回车

可以看到 <1,2><1,3><2,4>

希望可以帮到你

-DFAE -DESS -DDVD -DMTK  -DDVR -DDECODE -DMSTAR -DPMP我决定在论坛潜水3年又3年!
2007-12-19 19:06
liweiqing
Rank: 1
等 级:新手上路
帖 子:72
专家分:0
注 册:2007-10-16
收藏
得分:0 
非常感谢!!!!
帮助很大。

岂能尽如人意,但求无愧于心.
2007-12-25 22:36
快速回复:无向图的多重邻接表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027316 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved