图的数据结构1

#include <stdio.h>
#include<stdlib.h>
#define MaxN 6
int visited[MaxN]={0};
typedef struct ArcNode{
int adjvex;
double weight;
struct ArcNode *nextarc;
}EdgeNode;

typedef struct VNode{
char data;
struct ArcNode *firstarc;
}AdjList[MaxN];

typedef struct Graph{
int Vnum;
AdjList Vertices;
}Graph;

void InitGraph(Graph *G,int i,int j)
{
EdgeNode *p;
p=(EdgeNode*)malloc(sizeof(EdgeNode));
p->adjvex=j;p->nextarc =NULL;
if(G->Vertices[i].firstarc==NULL)
G->Vertices[i].firstarc=p;
else
{
p->nextarc=G->Vertices [i].firstarc;
G->Vertices[i].firstarc=p;
}
}

void Dfs(Graph *G,int i)
{
EdgeNode *t;int j;
printf("%d",i);
visited[i]=1;
t=G->Vertices[i].firstarc;
while(t!=NULL)
{
j=t->adjvex;
if(visited[j]==0)
Dfs(G,j);
t=t->nextarc;
}
}

void main()
{
int i,j;
Graph *G;
G=(Graph*)malloc(sizeof(Graph));
for(i=0;i<MaxN;i++)
{
G->Vertices[i].firstarc=NULL;
}
while(1)
{
scanf("%d%d",&i,&j);
if(i==0&&j==0) break;
InitGraph(G,i,j);
}
for(i=0;i<MaxN;i++)
Dfs(G,i);
}
Live together,or Die alone!
原文地址:https://www.cnblogs.com/hzhida/p/2354732.html