-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCListGraph.cpp
49 lines (41 loc) · 1.24 KB
/
CListGraph.cpp
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
#include <iostream>
#include <vector>
#include <list>
#include <assert.h>
#include "CListGraph.h"
CListGraph::CListGraph(int vertexNum) {
vertexCount = vertexNum;
adjLists = new std::list<int>[vertexCount];
adjListsReverse = new std::list<int>[vertexCount];
}
CListGraph::CListGraph(const IGraph* graph): CListGraph(graph->VerticesCount()) {
std::vector<int> temp;
for (int i = 0; i < vertexCount; i++) {
graph->GetNextVertices(i, temp);
for (unsigned int j = 0; j < temp.size(); j++) {
this->AddEdge(i, temp[j]);
}
temp.clear();
}
}
CListGraph::~CListGraph() {
delete[] adjLists;
delete[] adjListsReverse;
}
int CListGraph::VerticesCount() const {
return vertexCount;
}
void CListGraph::AddEdge(int from, int to) {
adjLists[from].push_back(to);
adjListsReverse[to].push_back(from);
}
void CListGraph::GetNextVertices(int vertex, std::vector<int>& vertices) const {
for (std::list<int>::const_iterator it = adjLists[vertex].begin(); it != adjLists[vertex].end(); it++) {
vertices.push_back(*it);
}
}
void CListGraph::GetPrevVertices(int vertex, std::vector<int>& vertices) const {
for (std::list<int>::const_iterator it = adjListsReverse[vertex].begin(); it != adjListsReverse[vertex].end(); it++) {
vertices.push_back(*it);
}
}