#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define MAX 20
typedef struct Anode
{
int adj;
struct Anode *next;
}Anode;
typedef struct Vnode
{
int data;
Anode *first;
}Vnode,Adj[MAX];
typedef struct
{
Adj vertices;
int vexnum,arcnum;
}Graph;
int LocateVex(Graph *G,int v)
{
int n=0;
while(G->vertices[n].data!=v)
{
n++;
}
return n;
}
void CreateGraph(Graph *G)
{
int i,j,m;
int v1,v2;
Anode *a1,*a2;
printf("input num of vex and arc:\n");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("input value of vex:\n");
for(i=0;i<G->vexnum;i++)
{
scanf("%d",&G->vertices[i].data);
G->vertices[i].first=NULL;
}
printf("input v1 and v2:\n");
for(m=0;m<G->arcnum;m++)
{
scanf("%d,%d",&v1,&v2);
i=LocateVex(G,v1);
j=LocateVex(G,v2);
a1=(Anode *)malloc(sizeof(Anode));
if(!a1)
printf("ERROR!\n");
if(!G->vertices[i].first)
{
G->vertices[i].first=a1;
a1->adj=j;
a1->next=NULL;
a2=a1;
}
else
{
a2->next=a1;
a1->adj=j;
a1->next=NULL;;
a2=a1;
}
}
}
void Out_Graph(Graph *G)
{
Anode *a1;
int i;
printf("output:\n");
for(i=0;i<G->vexnum;i++)
{
printf("%d",G->vertices[i].data);
printf("\t");
a1=G->vertices[i].first;
while(a1)
{
printf("%d\t",a1->adj);
a1=a1->next;
}
printf("\n");
}
}
int main()
{
Graph G;
CreateGraph(&G);
Out_Graph(&G);
return 0;
}
![]()
![]()
![]()
浙公网安备 33010602011771号