-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjListGraph.hpp
214 lines (193 loc) · 5.75 KB
/
adjListGraph.hpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// adjListGraph.hpp
// datastructure
//
// Created by duchunhui on 2018/12/16.
// Copyright © 2018 duchunhui. All rights reserved.
//
#ifndef adjListGraph_hpp
#define adjListGraph_hpp
#include <stdio.h>
#include <iostream>
#include "graph.hpp"
#include "linkQueue.hpp"
using namespace std;
template<class TypeOfVer, class TypeOfEdge>
class adjListGraph:public graph<TypeOfVer, TypeOfEdge>{
public:
adjListGraph(int vSize, const TypeOfVer d[]);
void insert(TypeOfVer x, TypeOfVer y, TypeOfEdge w);
void remove(TypeOfVer x, TypeOfVer y);
bool exist(TypeOfVer x, TypeOfVer y) const;
~adjListGraph();
void dfs() const;
void bfs() const;
void findPath(TypeOfVer start, int m) const;
private:
struct edgeNode
{
int end;
TypeOfEdge weight;
edgeNode *next;
edgeNode(int e, TypeOfEdge w, edgeNode *n=NULL):end(e), weight(w), next(n){}
};
struct verNode
{
TypeOfVer ver;
edgeNode *head;
verNode(edgeNode *h=NULL):head(h){}
};
verNode *verList;
int find(TypeOfVer v) const{
for(int i=0; i<this->Vers; ++i){
if (verList[i].ver==v)
return i;
}
return 0;
}
void dfs(int start, bool visited[]) const;
void findPath(int start, int m, int &top, bool visited[], int st[]) const;
};
template<class TypeOfVer, class TypeOfEdge>
adjListGraph<TypeOfVer, TypeOfEdge>::adjListGraph(int vSize, const TypeOfVer d[]){
this->Vers = vSize;
this->Edges = 0;
verList = new verNode[this->Vers];
for(int i=0; i<this->Vers; ++i)
verList[i].ver = d[i];
}
template<class TypeOfVer, class TypeOfEdge>
adjListGraph<TypeOfVer, TypeOfEdge>::~adjListGraph<TypeOfVer, TypeOfEdge>(){
edgeNode *p;
for (int i=0; i<this->Vers; ++i) {
while ((p=verList[i].head)!=NULL) {
verList[i].head = p->next;
delete p;
}
}
delete [] verList;
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::insert(TypeOfVer x, TypeOfVer y, TypeOfEdge w){
int u = find(x), v = find(y);
verList[u].head = new edgeNode(v, w, verList[u].head);
++this->Edges;
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::remove(TypeOfVer x, TypeOfVer y){
int u = find(x), v = find(y);
edgeNode *p = verList[u].head, *q;
if (p==NULL)
return;
if (p->end==v){//first node of list is to be deleted
verList[u].head = p->next;
delete p;
--this->Edges;
return;
}
while (p->next!=NULL&&p->next->end!=v)//judge p->next rather than p
p = p->next;
if (p->next!=NULL) {
q = p->next;
p->next = q->next;
delete q;
--this->Edges;
}
}
template<class TypeOfVer, class TypeOfEdge>
bool adjListGraph<TypeOfVer, TypeOfEdge>::exist(TypeOfVer x, TypeOfVer y) const{
int u = find(x), v = find(y);
edgeNode *p = verList[u].head;
while (p!=NULL&&p->end!=v)
p = p->next;
return p!=NULL;
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::dfs() const{
int i;
bool *visited = new bool[this->Vers];
for(i=0; i<this->Vers; ++i) visited[i] = false;
for (i=0; i<this->Vers; ++i) {
if (visited[i]==false) {
dfs(i, visited);
cout << endl;
}
}
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::dfs(int start, bool *visited) const{
edgeNode *p = verList[start].head;
cout << verList[start].ver << '\t';
visited[start] = true;
while (p!=NULL) {
if (visited[p->end]==false)
dfs(p->end, visited);
p = p->next;
}
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::bfs() const{
int currentNode;
edgeNode *p;
linkQueue<int> que;
bool *visited = new bool[this->Vers];
for (int i=0; i<this->Vers; ++i) visited[i] = false;
cout << "bfs sequence is:" << endl;
for (int j=0; j<this->Vers; ++j) {
if (visited[j]==false){
que.enQueue(j);
visited[j] = true;
}
while (!que.isEmpty()) {
currentNode = que.deQueue();
cout << verList[currentNode].ver << '\t';
p = verList[currentNode].head;
while (p!=NULL) {
if (visited[p->end]==false) {
que.enQueue(p->end);
visited[p->end] = true;
}
p = p->next;
}
}
cout << endl;
}
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::findPath(TypeOfVer start, int m) const{
bool *visited = new bool[this->Vers];
int *stack = new int[m + 1], top = 0;
int i;
for (i=0; i<this->Vers; ++i) visited[i] = false;
for (i=0; i<this->Vers; ++i) {
if (verList[i].ver==start)
break;
}
if (i==this->Vers){
cout << "start is not exist" << endl;
return;
}
findPath(i, m, top, visited, stack);
}
template<class TypeOfVer, class TypeOfEdge>
void adjListGraph<TypeOfVer, TypeOfEdge>::findPath(int start, int m, int &top, bool *visited, int *st) const{
edgeNode *p = verList[start].head;
visited[start] = true;
st[top++] = start;
if (top==m+1) {
cout << endl;
for (int i=0; i<top; ++i)
cout << verList[st[i]].ver << '\t';
visited[start] = false;
--top;
return;
}
while (p!=NULL) {
if (!visited[p->end])
findPath(p->end, m, top, visited, st);
p = p->next;
}
visited[start] = false;
--top;
}
#endif /* adjListGraph_hpp */