liweiqing 发表于 2007-12-13 22:42

无向图的多重邻接表

跪求:::无向图的多重邻接表!!!!!

liweiqing 发表于 2007-12-17 18:24

[em05]

liweiqing 发表于 2007-12-17 18:26

哎!!!

kelifei 发表于 2007-12-19 19:06

题目:编写一个算法由依次输入的顶点数目,边的数目,各顶点的信息和各条边的信息建立无向图的邻接多重表。

一.   需求分析

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

二.   概要设计

首先是建立两个结点,一个是边结点,另一个是顶点结点,分别为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>

希望可以帮到你

liweiqing 发表于 2007-12-25 22:36

非常感谢!!!!
帮助很大。

页: [1]

编程论坛