#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define MVNum 100
typedef int OtherInfo;
typedef char VerTexType;
typedef struct ArcNode{
int adjvex;
struct ArcNode*nextarc;
OtherInfo info;
}ArcNode;
typedef struct VNode{
VerTexType data;
ArcNode*firstarc;
}VNode,AdjList[MVNum];
typedef struct{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
int LocateNode(ALGraph G,VerTexType v){
int i;
for(i=0;i<G.vexnum;i++){
if(v==G.vertices[i].data){
return i;
}
}
return 0;
}
int CreateGraph(ALGraph *G){
printf("------开始创建无向网------\n");
printf("请输入顶点数和边数:");
scanf("%d %d",&G->vexnum,&G->arcnum);
fflush(stdin);
int n;
for(n=0;n<G->vexnum;n++){
printf("\n请输入第%d个顶点:",n+1);
G->vertices[n].data=getche();
G->vertices[n].firstarc=NULL;
}
int i,j;
for(n=0;n<G->arcnum;n++){
printf("\n请输入第%d条边关联的顶点和权值,以空格间隔:",n+1);
fflush(stdin);
VerTexType v1,v2;
OtherInfo w;
scanf("%c %c %d",&v1,&v2,&w);
i=LocateNode(*G,v1);
j=LocateNode(*G,v2);
ArcNode *p1=(ArcNode *)malloc(sizeof(ArcNode));
p1->adjvex=j;
p1->info=w;
p1->nextarc=G->vertices[i].firstarc;
G->vertices[i].firstarc=p1;
ArcNode *p2=(ArcNode *)malloc(sizeof(ArcNode));
p2->adjvex=i;
p2->info=w;
p2->nextarc=G->vertices[j].firstarc;
G->vertices[j].firstarc=p2;
}
return 0;
}
int ShowGraph(ALGraph *G)
{int i;
for (i=0;i<G->vexnum;i++)
{
printf("%d->",i+1);
while(1)
{
if(G->vertices[i].firstarc==NULL)
{
printf("^");
break;
}
printf("%d->",G->vertices[i].firstarc->adjvex+1);
G->vertices[i].firstarc=G->vertices[i].firstarc->nextarc;
}
printf("\n");
}
}
int main()
{
ALGraph *G;
CreateGraph(G);
ShowGraph(G);
return 0;
}