#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
typedef struct ArcNode
{
int adjbex;
struct ArcNode *next;
} ArcNode;
typedef struct VNode
{
char data;
ArcNode *first;
} VNode, AdjList[MaxVertexNum];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
void InitGraph(ALGraph *G)
{
G->vexnum = 0;
G->arcnum = 0;
for (int i = 0; i < MaxVertexNum; i++)
{
G->vertices[i].data = ' ';
G->vertices[i].first = NULL;
}
}
void CreateGraph(ALGraph *G)
{
printf("Enter the number of vertices: ");
scanf("%d", &G->vexnum);
printf("Enter the number of edges: ");
scanf("%d", &G->arcnum);
for (int i = 0; i < G->vexnum; i++)
{
printf("Enter the data for vertex %d: ", i);
scanf(" %c", &G->vertices[i].data);
}
for (int i = 0; i < G->arcnum; i++)
{
int start, end;
printf("Enter the start and end vertices of edge %d: ", i);
scanf("%d %d", &start, &end);
ArcNode *arc = (ArcNode *)malloc(sizeof(ArcNode));
arc->adjbex = end;
arc->next = G->vertices[start].first;
G->vertices[start].first = arc;
arc = (ArcNode *)malloc(sizeof(ArcNode));
arc->adjbex = start;
arc->next = G->vertices[end].first;
G->vertices[end].first = arc;
}
}
int main()
{
ALGraph G;
InitGraph(&G);
CreateGraph(&G);
for (int i = 0; i < G.vexnum; i++)
{
printf("Vertex %c: ", G.vertices[i].data);
ArcNode *p = G.vertices[i].first;
while (p != NULL)
{
printf("%c ", G.vertices[p->adjbex].data);
p = p->next;
}
printf("\n");
}
return 0;
}