-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgraph.js
54 lines (47 loc) · 1.17 KB
/
graph.js
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
'use strict';
function Rib(vertex1, vertex2, length) {
this.begin = vertex1;
this.end = vertex2;
this.len = length;
}
function Graph() {
this.ribs = new Array();
}
Graph.prototype.add = function(vertex1, vertex2, length) {
if (!this.find(vertex1, vertex2, length)) {
const rib1 = new Rib(vertex1, vertex2, length);
this.ribs.push(rib1);
} else console.log('Rib is here!');
};
Graph.prototype.remove = function(vertex1, vertex2, length) {
let i;
for (i = 0; i < this.ribs.length; i++) {
if (this.ribs[i].begin === vertex1) {
if (this.ribs[i].end === vertex2) {
if (this.ribs[i].len === length) this.ribs.splice(i, 1);
}
}
}
};
Graph.prototype.find = function(vertex1, vertex2, length) {
let i;
for (i = 0; i < this.ribs.length; i++) {
if (this.ribs[i].begin === vertex1) {
if (this.ribs[i].end === vertex2) {
if (this.ribs[i].len === length) return true;
}
}
return false;
}
};
Graph.prototype.paint = function() {
let i;
for (i = 0; i < this.ribs.length; i++) {
console.log(this.ribs[i]);
}
};
const Ass = new Graph();
Ass.add(1, 2, 3);
Ass.add(2, 4, 5);
Ass.remove(2, 4, 5);
Ass.paint();