#include<stdio.h>
#define vnum 10
typedef struct arcnode
{
int adjvex;
struct arcnode * nextarc;
}ArcNode;
typedef struct vexnode
{
int vertex;
ArcNode *firstarc;
}AdjList[vnum];
typedef struct gp
{
AdjList adjlist;
int vexnum,arcnum;
}Graph;
CreateAdjlist(Graph *g,int x,int y)
{
int i;
g->vexnum=x;
g->arcnum=y;
for(i=0;i<g->vexnum;i++)
{
g->adjlist[i].vertex=i;
g->adjlist[i].firstarc=NULL;
}
}
InsertEdge(Graph *g)
{
ArcNode *p;
int i,j,k;
for(k=0;k<g->arcnum;k++)
{
printf("输入第%d条边:",k+1);
scanf("%d %d",&i,&j);
p=(ArcNode *)malloc(sizeof(ArcNode));
p->adjvex=j;
p->nextarc=g->adjlist[i].firstarc;
g->adjlist[i].firstarc=p;
}
}
OutGraph(Graph *g)
{
int i;
for(i=0;i<g->vexnum;i++)
{
printf("[%d]:",i);
while(g->adjlist[i].firstarc!=0)
{
printf("%d ",g->adjlist[i].firstarc->adjvex);
g->adjlist[i].firstarc=g->adjlist[i].firstarc->nextarc;
}
printf("\n");
}
}
main()
{
Graph g;
int x,y;
printf("输入顶点和边的个数:\n");
scanf("%d %d",&x,&y);
CreateAdjlist(&g,x,y);
InsertEdge(&g);
OutGraph(&g);
}