typedef char VertexType;
typedef int EdgeType;
#define MAXVEX 100
#define INFINITY 65535
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numNodes, numEdges;
}MGraph;
void CreateMGraph(MGraph *G)
{
int i,j,k,w;
printf("输入顶点数和边数:\n");
scanf("%d,%d",&G->numNodes,&G->numEdges);
for(i = 0;i <G->numNodes;i++)
scanf(&G->vexs[i]);
for(i = 0;i <G->numNodes;i++)
for(j = 0;j <G->numNodes;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 char VertexType;
typedef int EdgeType;
typedef struct EdgeNode
{
int adjvex;
EdgeType info;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
``
```c
typedef struct
{
AdjList adjList;
int numNodes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *G)
{
int i,j,k;
EdgeNode *e;
printf("输入顶点数和边数:\n");
scanf("%d,%d",&G->numNodes,&G->numEdges);
for(i = 0;i < G->numNodes;i++)
{
scanf(&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;
}
}
#define MAXVEX 9
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);
}
void BFSTraverse(MGraph G)
{
int i, j;
Queue Q;
for(i = 0; i < G.numVertexes; i++)
visited[i] = FALSE;
InitQueue(&Q);
for(i = 0; i < G.numVertexes; i++)
{
if (!visited[i])
{
visited[i]=TRUE;
printf("%c ", G.vexs[i]);
EnQueue(&Q,i);
while(!QueueEmpty(Q))
{
DeQueue(&Q,&i);
for(j=0;j<G.numVertexes;j++)
{
if(G.arc[i][j] == 1 && !visited[j])
{
visited[j]=TRUE;
printf("%c ", G.vexs[j]);
EnQueue(&Q,j);
}
}
}
}
}
}
void BFSTraverse(GraphAdjList GL)
{
int i;
EdgeNode *p;
Queue Q;
for(i = 0; i < GL->numVertexes; i++)
visited[i] = FALSE;
InitQueue(&Q);
for(i = 0; i < GL->numVertexes; i++)
{
if (!visited[i])
{
visited[i]=TRUE;
printf("%c ",GL->adjList[i].data);
EnQueue(&Q,i);
while(!QueueEmpty(Q))
{
DeQueue(&Q,&i);
p = GL->adjList[i].firstedge;
while(p)
{
if(!visited[p->adjvex])
{
visited[p->adjvex]=TRUE;
printf("%c ",GL->adjList[p->adjvex].data);
EnQueue(&Q,p->adjvex);
}
p = p->next;
}
}
}
}
}