数组与链表结合的存储方法称为
邻接表
贴下自己的代码:
#include
using namespace std;
const int MAXVEX = 20;
//设最大的结点个数为20
struct EdgeNode
//建立边表结点
{
int
adjvex;
//邻接序号
int
weight;
//注意:对于带权值的网图,可以在边表结点定义中增加weight的数据域,存储权值信息。
EdgeNode
*
next;
};
typedef struct VertexNode
//建立顶点表结点
{
char
data;
//顶点域,存储顶点信息
EdgeNode
*
firsttedge;
//边表头指针
} AdjList [ MAXVEX ];
struct Graph
{
AdjList
adjlist;
int
vex_num
,
edg_num;
//图中的结点数和边数
};
void CreatGraph( Graph & G)
{
int
i
,
j
,
k;
EdgeNode
*
e;
cout
<<
"请输入顶点数和边数:"
<<
endl;
cin
>>
G
.
vex_num
>>
G
.
edg_num;
cout
<<
"请输入顶点表信息:"
<<
endl;
for(
i
=
0;
i
<</span> G.vex_num ; i ++) //初始化顶点表的信息
{
cout<<"V"<<i<<":";
cin>>G.adjlist[i].data; //输入顶点表,建立顶点表
G.adjlist[i].firsttedge = NULL; //将边表置为空表
}
for( k =0 ; k <</span>G.edg_num ;k ++) //建立边表信息
{
cout<<"输入边(Vi,Vj)的顶点序号i,j"<<endl;
cin>>i>>j;
e = new EdgeNode; //生成边表结点
e->adjvex = j; //邻接序号为j
e->next = G.adjlist[i].firsttedge; //将e指针指向当前结点指向的结点
G.adjlist[i].firsttedge = e; //将当前结点指针指向e
e = new EdgeNode;
e->adjvex = i;
e->next = G.adjlist[j].firsttedge;
G.adjlist[j].firsttedge = e;
}
cout<<"creat successful"<<endl;
}
//插入过程用了单链表的头插法
int main()
{
Graph G;
CreatGraph(G);
}
以无向图为例
举个小例子:无向图的邻接表结构
贴下自己的代码:
#include
using namespace std;
const int MAXVEX = 20;
struct EdgeNode
{
};
typedef struct VertexNode
{
} AdjList [ MAXVEX ];
struct Graph
{
};
void CreatGraph( Graph & G)
{
}
//插入过程用了单链表的头插法
int main()
{
}