#include<iostream>
using namespace std;
#define MAX_V_NUM 20
typedef int vertextype;
typedef int arctype;
typedef char infotype;
typedef struct ArcNode{
ArcNode* next;
int index_v;
infotype* info;
}ArcNode;
typedef struct{
ArcNode* first;
vertextype v;
}VerNode;
typedef struct{
VerNode adjlist[MAX_V_NUM];
int vernum,arcnum;
}Graph;
void creategraph(Graph &g)
{
char* info;
int i,j,k;
ArcNode *p;
cout<<"请输入顶点数与边数:";
cin>>g.vernum>>g.arcnum;
for(i=0;i<g.vernum;i++){//初始化
g.adjlist[i].first=NULL;
g.adjlist[i].v=i;
}
for(k=0;k<g.arcnum;k++){
cout<<"输入i,j以及info:";//输入info太麻烦了,这里就不用输入了
cin>>i>>j;//直接输入i,j
p=new ArcNode;
p->next=g.adjlist[i].first;
g.adjlist[i].first=p;
p->info=info;
p->index_v=j;
p=new ArcNode;
p->next=g.adjlist[j].first;
g.adjlist[j].first=p;
p->info=info;
p->index_v=i;
}
}
void showgraph(Graph g)
{
int i;
ArcNode* p;
for(i=0;i<g.vernum;i++){
cout<<g.adjlist[i].v<<"\t";
p=g.adjlist[i].first;
while(p!=NULL){
cout<<p->index_v<<"\t";
p=p->next;
}
cout<<endl;
}
}
int main()
{
Graph g;
creategraph(g);
showgraph(g);
return 0;
}
演示如下: