Skip to content

Commit 060d51e

Browse files
committedDec 10, 2019
update: graph implementation, filter in LL, eslit & prettier fix
1 parent dce3870 commit 060d51e

File tree

6 files changed

+451
-188
lines changed

6 files changed

+451
-188
lines changed
 

‎.eslintrc.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"node": true,
44
"jest": true
55
},
6-
"extends": ["prettier", "airbnb-base"],
7-
"plugins": ["prettier"]
6+
"extends": ["airbnb", "plugin:prettier/recommended", "plugin:node/recommended"],
7+
"plugins": ["prettier"],
8+
"rules": {
9+
"prettier/prettier": "error"
10+
}
811
}

‎.prettierrc.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = {
22
semi: true,
3-
trailingComma: "all",
3+
trailingComma: 'all',
44
singleQuote: true,
55
printWidth: 120,
6-
tabWidth: 2
6+
tabWidth: 2,
7+
arrowParens: 'always',
78
};

‎package-lock.json

+321-181
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
"author": "Ashok Dey <ashokdey100@gmail.com> (http://ashokdey.in)",
1717
"license": "MIT",
1818
"devDependencies": {
19-
"eslint": "^5.11.1",
19+
"eslint": "^5.16.0",
20+
"eslint-config-airbnb": "^18.0.1",
2021
"eslint-config-airbnb-base": "^13.1.0",
21-
"eslint-config-prettier": "^6.5.0",
22+
"eslint-config-node": "^4.0.0",
23+
"eslint-config-prettier": "^6.7.0",
2224
"eslint-plugin-import": "^2.14.0",
25+
"eslint-plugin-node": "^10.0.0",
2326
"eslint-plugin-prettier": "^3.1.1",
2427
"jest": "^25.0.0",
2528
"prettier": "^1.19.1"

‎src/_DataStructures_/Graphs/index.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const { LinkedList } = require('../LinkedList');
2+
3+
class Graph {
4+
constructor() {
5+
this.data = this.getStorage();
6+
}
7+
8+
addVertex(v) {
9+
this.data.addVertex(v);
10+
}
11+
12+
addEdge(v, e) {
13+
this.data.addEdge(v, e);
14+
}
15+
16+
removeEdge(v, e) {
17+
this.data.removeEdge(v, e);
18+
}
19+
20+
removeVertex(v) {
21+
this.data.removeVertex(v);
22+
}
23+
24+
getEdges(v) {
25+
return this.data.getEdges(v);
26+
}
27+
28+
displayMatrix() {
29+
return this.data.displayMatrix();
30+
}
31+
32+
// eslint-disable-next-line class-methods-use-this
33+
getStorage() {
34+
const map = {};
35+
36+
return {
37+
addVertex(v) {
38+
if (!map[v]) map[v] = new LinkedList();
39+
},
40+
addEdge(v, e) {
41+
if (map[v]) {
42+
map[v].addAtEnd(e);
43+
}
44+
},
45+
removeEdge(v, e) {
46+
if (map[v]) {
47+
map[v].filter(e);
48+
}
49+
},
50+
removeVertex(v) {
51+
if (map[v]) {
52+
delete map[v];
53+
54+
const vertices = Object.keys(map);
55+
const edge = v; // this vertex may be an edge for other vertices
56+
vertices.forEach((vertex) => this.removeEdge(vertex, edge));
57+
}
58+
},
59+
getEdges(v) {
60+
if (map[v]) {
61+
return map[v].traverseList();
62+
}
63+
},
64+
displayMatrix() {
65+
const vertices = Object.keys(map);
66+
const result = {};
67+
68+
vertices.forEach((v) => {
69+
result[v] = map[v].traverseList();
70+
});
71+
return result;
72+
},
73+
};
74+
}
75+
}
76+
77+
// const g = new Graph();
78+
79+
// g.addVertex('Noida');
80+
// console.log(g.displayMatrix());
81+
82+
// g.addEdge('Noida', 'Greater Noida');
83+
// g.addEdge('Noida', 'Ghaziabaad');
84+
// g.addEdge('Noida', 'Meerut');
85+
// g.addEdge('Noida', 'Greater Noida');
86+
// g.addEdge('Noida', 'Mathura');
87+
88+
// g.addVertex('Mathura');
89+
// g.addEdge('Mathura', 'Noida');
90+
// g.addEdge('Mathura', 'Meerut');
91+
// console.log(g.displayMatrix());
92+
// // g.data['Noida'].size = 10;
93+
94+
// // console.log(g.data['Noida']);
95+
// // g.filter('Noida', 'Greater Noida');
96+
// console.log(g.displayMatrix());
97+
// console.log('removing Mathura');
98+
99+
// g.removeVertex('Mathura');
100+
// console.log(g.displayMatrix());
101+
// console.log(g.getEdges('Noida'));
102+
103+
module.exports = Graph;

‎src/_DataStructures_/LinkedList/index.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,14 @@ class LinkedList {
168168
if (head.data === value) {
169169
previous.next = head.next;
170170
this.size -= 1;
171+
172+
if (head.next === null) {
173+
this.tail = previous;
174+
}
171175
}
172176
previous = head;
173177
head = head.next;
174178
}
175-
176179
return this.head;
177180
}
178181

@@ -211,4 +214,14 @@ class LinkedList {
211214
// console.log(ll.removeAt(1));
212215
// console.log(ll.traverseList());
213216

217+
// const list = new LinkedList();
218+
// [1, 2, 3, 5, 3, 6, 7, 10, 3].forEach(el => {
219+
// list.addAtEnd(el);
220+
// });
221+
222+
// console.log(list.traverseList());
223+
// list.filter(3);
224+
// console.log(list.traverseList());
225+
// console.log(list.tail);
226+
214227
module.exports = { LinkedList, Node };

0 commit comments

Comments
 (0)
Please sign in to comment.