马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct
{
char data;
EdgeNode *firstNext;
}VertexNode,AdjList[MAX];
typedef struct
{
AdjList adjList;
int numVertexes;
int numArcs;
}GraphAdjList;
int locate(GraphAdjList G,char c)
{
int i=0;
while(G.adjList[i++].data!=c && i<G.numVertexes);
if(i<G.numVertexes)
return i;
else return -1;
}
void CreateAdjList(GraphAdjList *G)
{
int i,j,k;
EdgeNode *p,*q;
char m,n;
printf("请输入顶点和弧的数目\n");
scanf("%d %d",&G->numVertexes,&G->numArcs);
for(k=0;k<G->numVertexes;k++)
{
printf("请输入第%d个顶点的信息\n",k);
scanf("%c",&G->adjList[k].data);
G->adjList[k].firstNext=NULL;
}
for(k=0;k<G->numArcs;k++)
{
printf("请按顺序输入弧的弧头v和弧尾w\n");
scanf("%c %c",&m,&n);
q=(EdgeNode *)malloc(sizeof(EdgeNode));
i=locate(G,m);
j=locate(G,n);
q->adjvex=j;
if( G->adjList[i].firstNext!=NULL )
G->adjList[i].firstNext=q;
else
{
p=G->adjList[i].firstNext->next;
while(p)
{
p=p->next;
}
p->next=q;
}
}
}
void print(GraphAdjList *G)
{
int i;
EdgeNode *p;
for(i=0;i<G->numVertexes;i++)
{
printf("%c",G->adjList[i].data);
p=G->adjList[i].firstNext;
while(p)
{
printf("->%d",p->adjvex);
p=p->next;
}
printf("->NULL\n");
}
free(p);
}
int main()
{
GraphAdjList *G;
CreateAdjList(G);
print(G);
return 0;
}
|