-
typedef char VertexType;
-
typedef int EdgeType;
-
#define MAXVEX 100
-
#define INFINITY 65535
-
-
typedef struct
-
{
-
VertexType vexs[MAXVEX];
-
EdgeType arc[MAXVEX][MAXVEX];
-
int numVertexes, numEdges;
-
}MGraph;
-
-
void CreateMGraph( MGraph *G)
-
{
-
int i,j,k,w;
-
printf(\"输入顶点数和变数:\\n\");
-
scanf(\"%d,%d\", &G->numVertexes, &G->numEdges);
-
for(i=0; i<G->numVertexes; i++)
-
{
-
scanf(\"%d\",&G->vexs[i]);
-
}
-
for(i=0; i<G->numVertexes; i++)
-
{
-
for(j=0; j<G->numVertexes; j++)
-
{
-
G->arc[i][j]=INFINITY;
-
}
-
}
-
for(k=0; k<G->numEdges; k++)
-
{
-
printf(\"输入边(vi,vj)上的下标i,下标j和权w:\\n\");
-
scanf(\"%d,%d,%d\",&i,&j,&w);
-
G->arc[i][j]=w;
-
G->arc[j][i]=G->arc[i][j];
-
}
-
}
-
-
typedef struct EdgeNode
-
{
-
int adjvex;
-
EdgeType weight;
-
struct EdgeNode *next;
-
}EdgeNode;
-
-
typedef struct VertexNode
-
{
-
VertexType data;
-
EdgeNode *firstedge;
-
}VertexNode, AdjList[MAXVEX];
-
-
typedef struct {
-
-
AdjList adjList;
-
int numVertexes, numEdges;
-
-
}GraphAdjList;
-
-
void CreateALGraph(GraphAdjList *G)
-
{
-
int i,j,k;
-
EdgeNode *e;
-
printf(\"输入顶点数和边数:\\n\");
-
scanf(\"%d,%d\",&G->numVertexes,&G->numEdges);
-
for(i=0; i<G->numVertexes; i++)
-
{
-
scanf(\"%d\",&G->adjList[i].data);
-
G->adjList[i].firstedge=NULL;
-
}
-
for(k=0; k<G->numEdges; k++)
-
{
-
printf(\"输入边(vi,vj)上的顶点序号:\\n\");
-
scanf(\"%d,%d\",&i,&j);
-
e=(EdgeNode *)malloc(sizeof(EdgeNode));
-
e->adjvex=j;
-
e->next=G->adjList[i].firstedge;
-
G->adjList[i].firstedge=e;
-
e=(EdgeNode *)malloc(sizeof(EdgeNode));
-
e->adjvex=i;
-
e->next=G->adjList[j].firstedge;
-
G->adjList[j].firstedge=e;
-
}
-
}
-
-
typedef int Boolean;
-
Boolean visited[MAXVEX];
-
-
void DFS(MGraph G, int i)
-
{
-
int j;
-
visited[i]=true;
-
printf(\"%c\",G.vexs[i]);
-
for(j=0; j<G.numVertexes; j++)
-
{
-
if(G.arc[i][j]==1&&!visited[j])
-
{
-
DFS(G, j);
-
}
-
}
-
-
}
-
-
void DFSTraverse(MGraph G)
-
{
-
int i;
-
for(i=0; i<G.numVertexes; i++)
-
visited[i]=false;
-
for(i=0; i<G.numVertexes; i++)
-
{
-
if(!visited[i])
-
{
-
DFS(G,i);
-
}
-
}
-
}
-
-
void DFS(GraphAdjList *GL, int i)
-
{
-
EdgeNode *p;
-
visited[i]=true;
-
printf(\"%c\", GL->adjList[i].data);
-
p=GL->adjList[i].firstedge;
-
while(p)
-
{
-
if(!visited[p->adjvex])
-
{
-
DFS(GL, p->adjvex);
-
}
-
p=p->next;
-
}
-
}
-
-
void DFSTraverse(GraphAdjList *GL)
-
{
-
int i;
-
for(i=0; i<GL->numVertexes; i++)
-
{
-
visited[i]=false;
-
}
-
for(i=0; i<GL->numVertexes; i++)
-
{
-
if(!visited[i])
-
{
-
DFS(GL, i);
-
}
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1143032/
,如需转载,请注明出处,否则将追究法律责任。