#define _CRT_SECURE_NO_WARNINGS 1
//图--邻接表
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct EdgeNode //边表结点
{
int adjvex;//邻接点域
int info;//权值
struct EdgeNode* next;
}EdgeNode;
typedef struct VertexNode//顶点表结点
{
char data;//顶点域
EdgeNode* fistedge;//边头指针
}VertexNode,AdjList[1000];
typedef struct
{
AdjList adjlist;
int numNode, numEdge;//顶点和边数
}GraphAdjList;
void CreateALGraph(GraphAdjList* G) //创建邻接表
{
int i, j, k;
EdgeNode* e;
printf("输入顶点数和边数:\n");
scanf("%d %d", &G->numNode, &G->numEdge);
printf("输入顶点:\n");
for (i = 0; i < G->numNode; i++) //建立顶点表
{
getchar();
scanf("%c", &G->adjlist[i].data);//顶点信息
G->adjlist[i].fistedge = NULL;
}
for (k = 0; k < G->numEdge; k++) //建立边表
{
printf("输入边(v1,v2)上的顶点序号:\n");
scanf("%d %d", &i, &j);
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = j;
e->next = G->adjlist[i].fistedge;
G->adjlist[i].fistedge = e;
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = i;
e->next = G->adjlist[j].fistedge;
G->adjlist[j].fistedge = e;
}
}
void ALGraphPrint(GraphAdjList* G)
{
EdgeNode* p;
printf("输出邻接表:\n");
for (int i = 0; i < G->numNode; i++)
{
printf("%c", G->adjlist[i].data);
for (p = G->adjlist[i].fistedge; p != NULL; p = p->next)
{
printf("->%d", p->adjvex);
}
printf("\n");
}
}
int main()
{
GraphAdjList G;
CreateALGraph(&G);
ALGraphPrint(&G);
return 0;
}