图的邻接表创建方法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

完整代码如下:
//下面介绍图的邻接表创建方法
typedef struct edgenode
{
int Vertex; //vertex number
int weight; //quan
struct edgenode *next; //the next edgenode
}EdgeNode;

//图的邻接链表定义
typedef struct
{
EdgeNode* Adjlist[MAXNUM];
int vertex_num;
int edge_num;
int Graphtype;
}ListGraph;

//生成图的邻接表
void Creatgraph(ListGraph *G)
{
int i,weight,start,end;
EdgeNode *s;
for(i=0;ivertex_num;i++) //clear the node pointer
G->Adjlist[i]=NULL;
for(i=0;iedge_num;i++) //input the 2 node of each edge and weight
{
cout<<"the "<cin>>start>>end>>weight;
s=(EdgeNode *)malloc(sizeof(EdgeNode));
s->next=G->Adjlist[start];
s->Vertex=end;
s->weight=weight;
G->Adjlist[start]=s;
if(G->Graphtype == 0)
{
s=(EdgeNode *)malloc(sizeof(EdgeNode));
s->next=G->Adjlist[end];
s->Vertex=start;
s->weight=weight;
G->Adjlist[end]=s;
}
}
}

//输出邻接表
void OutList(ListGraph *G)
{
int i;
EdgeNode *s;
for(i=0;ivertex_num;i++)
{
cout<<"Vertex:"<s=G->Adjlist[i];
while(s)
{
cout<<"->"<Vertex<<"("<weight<<")";
s=s->next;
}
cout<}
}


主函数调用测试:

#include "stdafx.h"
#include "example32_graph.h"
#include
#include
int main()
{
ListGraph G;
cout<<"Is the graph have direction?(0/1)"<cin>>G.Graphtype;
cout<<"please input the vertex_number and edge number:"<cin>>G.vertex_num>>G.edge_num;
cout<<"input the two nodes of each edge and weight:"<
Creatgraph(&G);
cout<<"now the new matrix is follow:"<OutList(&G);
getch();
return 0;
}




相关文档
最新文档