每一个顶点后面就是一条链表,每个顶点都存在数组里。 以这张图为例 结构如下 运行截图 结构体定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 //边表结点
typedef struct EdgeNode {
//顶点对应的下标
int adjvex;
//指向下一个邻接点
struct EdgeNode *next;
} edgeNode;

//顶点表结点
typedef struct VertexNode {
//顶点数据
char data;
//边表头指针
edgeNode *firstedge;
} VertexNode, AdjList[100];

//集合
typedef struct {
AdjList adjList;
//顶点数和边数
int numVertexes, numEdges;
} GraphAdjList;

完整程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 #include <iostream>

using namespace std;
//边表结点
typedef struct EdgeNode {
//顶点对应的下标
int adjvex;
//指向下一个邻接点
struct EdgeNode *next;
} edgeNode;

//顶点表结点
typedef struct VertexNode {
//顶点数据
char data;
//边表头指针
edgeNode *firstedge;
} VertexNode, AdjList[100];

//集合
typedef struct {
AdjList adjList;
//顶点数和边数
int numVertexes, numEdges;
} GraphAdjList;

class AdjacencyList {
public:

void CreateALGraph(GraphAdjList *G);

void ShowALGraph(GraphAdjList *G);

void Test();
};

void AdjacencyList::CreateALGraph(GraphAdjList *G) {
int i, j, k;
edgeNode *e;
cout << "输入顶点数和边数" << endl;
//输入顶点数和边数
cin >> G->numVertexes >> G->numEdges;
//读入顶点信息,建立顶点表
for (i = 0; i < G->numVertexes; i++)
{
//输入顶点信息
cin >> G->adjList[i].data;
//将边表置为空表
G->adjList[i].firstedge = NULL;
}
//建立边表(头插法)
for (k = 0; k < G->numEdges; k++)
{
cout << "输入边(vi,vj)上的顶点序号" << endl;
cin >> i >> j;
e = new EdgeNode;
e->adjvex = j;
e->next = G->adjList[i].firstedge;
G->adjList[i].firstedge = e;

e = new EdgeNode;

e->adjvex = i;
e->next = G->adjList[j].firstedge;
G->adjList[j].firstedge = e;
}
}

void AdjacencyList::Test() {
cout << "ALL IS OK." << endl;
}

void AdjacencyList::ShowALGraph(GraphAdjList *G) {
for (int i = 0; i < G->numVertexes; i++)
{
cout << "顶点" << i << ": " << G->adjList[i].data<<"--firstedge--";
edgeNode *p = new edgeNode;
p = G->adjList[i].firstedge;
while (p)
{
cout<<p->adjvex<<"--Next--";
p=p->next;
}
cout<<"--NULL"<<endl;
}

}

int main() {
AdjacencyList adjacencyList;
GraphAdjList *GA = new GraphAdjList;
adjacencyList.Test();
adjacencyList.CreateALGraph(GA);
adjacencyList.ShowALGraph(GA);
return 0;
}