麦茜 威廉姆斯
#include<stdio.h>
#define MAX_VERTEX_NUM 20
#define ERROR -1
#define TRUE 1
#define FALSE 0
inferno
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
char data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
preventtypedef struct {
我回来了日语 AdjList vertices;
int vexnum,arcnum;
}ALGraph;
void CreateAL(ALGraph *G);
int LocateVex(ALGraph G,char u);
void DFSTraver(ALGraph G,void (*Visit)(ALGraph G,int v));
void PrintElem(ALGraph G,int v);
void DFS(ALGraph G,int v);
int FirstAdjVex(ALGraph G,int v);
int NextAdjVex(ALGraph G,int v,int w);
名古屋产业大学
int visited[MAX_VERTEX_NUM];
void (*VisitFunc)(ALGraph G,int v);
int main(){
ALGraph G;
attend过去式 CreateAL(&G);
printf("The Graph is:\n");
DFSTraver(G,PrintElem);
getch();
}
void CreateAL(ALGraph *T){
int i,j,m;
ArcNode *p,*s;
char ch[100];
printf("Plea input the vexnum and arcnumm:\n");
scanf("%d%d",&(T->vexnum),&(T->arcnum));
printf("Plea input the vertexs:\n");
for(i=0;i<(T->vexnum);++i){
scanf(" %c",&(T->vertices[i].data));
T->vertices[i].firstarc=NULL;
licenplate }
for(i=0;i<(T->vexnum);++i){
m=0;
printf("Plea input the adjacents of %c\n",T->vertices[i].data);
scanf(" %s",&ch);
j=LocateVex(*T,ch[0]);
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=NULL;
T->vertices[i].firstarc=p;
while(ch[++m]!='\0'){
j=LocateVex(*T,ch[m]);
s=(ArcNode *)malloc(sizeof(ArcNode));
s->adjvex=j;
s->nextarc=NULL;
p->nextarc=s;
p=s;
}
}
}
int LocateVex(ALGraph G,char u){
int j;
int i=sizeof(G.vertices)/sizeof(VNode);
2010考研英语 for(j=0;j<i;j++){
if(u==G.vertices[j].data)
return j;
}
printf("Can not find the vertex! Plea press any key to exit\n");
getchar();
getchar();
exit(ERROR);
}
void DFSTraver(ALGraph G,void (*Visit)(ALGraph G,int v)){
int v;
VisitFunc=Visit;
for(v=0;v<G.vexnum;++v)
visited[v]=FALSE;
for(v=0;v<G.vexnum;++v)
if(!visited[v]) DFS(G,v);
}
void DFS(ALGraph G,int v){
int w;
visited[v]=TRUE;
VisitFunc(G,v);
for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))
if(!visited[w]) DFS(G,w);
}
void PrintElem(ALGraph G,int v){北京英语培训机构
printf(" %3c",G.vertices[v].data);
}
int FirstAdjVex(ALGraph G,int v){
ArcNode *p=G.vertices[v].firstarc;实习生英文
return p->adjvex;
}
int NextAdjVex(ALGraph G,int v,int w){
ArcNode *p,*s;
for(p=G.vertices[v].firstarc;p!=NULL;p=p->nextarc){
if(((p->adjvex)==w)&&(p->nextarc)){
s=p->nextarc;
return s->adjvex;
}
}
return ERROR;
}
_