forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fomrat the JS and TS codes with prettier.
- Loading branch information
Showing
68 changed files
with
634 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tabWidth": 4, | ||
"useTabs": false, | ||
"semi": true, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* Author: IsChristina ([email protected]), Justin ([email protected]) | ||
*/ | ||
|
||
const { printLinkedList } = require("../modules/PrintUtil"); | ||
const { ListNode } = require("../modules/ListNode"); | ||
const { printLinkedList } = require('../modules/PrintUtil'); | ||
const { ListNode } = require('../modules/ListNode'); | ||
|
||
/* 在链表的节点 n0 之后插入节点 P */ | ||
function insert(n0, P) { | ||
|
@@ -16,8 +16,7 @@ function insert(n0, P) { | |
|
||
/* 删除链表的节点 n0 之后的首个节点 */ | ||
function remove(n0) { | ||
if (!n0.next) | ||
return; | ||
if (!n0.next) return; | ||
// n0 -> P -> n1 | ||
const P = n0.next; | ||
const n1 = P.next; | ||
|
@@ -61,23 +60,23 @@ n0.next = n1; | |
n1.next = n2; | ||
n2.next = n3; | ||
n3.next = n4; | ||
console.log("初始化的链表为"); | ||
console.log('初始化的链表为'); | ||
printLinkedList(n0); | ||
|
||
/* 插入节点 */ | ||
insert(n0, new ListNode(0)); | ||
console.log("插入节点后的链表为"); | ||
console.log('插入节点后的链表为'); | ||
printLinkedList(n0); | ||
|
||
/* 删除节点 */ | ||
remove(n0); | ||
console.log("删除节点后的链表为"); | ||
console.log('删除节点后的链表为'); | ||
printLinkedList(n0); | ||
|
||
/* 访问节点 */ | ||
const node = access(n0, 3); | ||
console.log("链表中索引 3 处的节点的值 = " + node.val); | ||
console.log('链表中索引 3 处的节点的值 = ' + node.val); | ||
|
||
/* 查找节点 */ | ||
const index = find(n0, 2); | ||
console.log("链表中值为 2 的节点的索引 = " + index); | ||
console.log('链表中值为 2 的节点的索引 = ' + index); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Author: Justin ([email protected]) | ||
*/ | ||
|
||
const { Vertex } = require('../modules/Vertex') | ||
const { Vertex } = require('../modules/Vertex'); | ||
|
||
/* 基于邻接表实现的无向图类 */ | ||
class GraphAdjList { | ||
|
@@ -29,8 +29,12 @@ class GraphAdjList { | |
|
||
/* 添加边 */ | ||
addEdge(vet1, vet2) { | ||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) { | ||
throw new Error("Illegal Argument Exception"); | ||
if ( | ||
!this.adjList.has(vet1) || | ||
!this.adjList.has(vet2) || | ||
vet1 === vet2 | ||
) { | ||
throw new Error('Illegal Argument Exception'); | ||
} | ||
// 添加边 vet1 - vet2 | ||
this.adjList.get(vet1).push(vet2); | ||
|
@@ -39,8 +43,12 @@ class GraphAdjList { | |
|
||
/* 删除边 */ | ||
removeEdge(vet1, vet2) { | ||
if (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) { | ||
throw new Error("Illegal Argument Exception"); | ||
if ( | ||
!this.adjList.has(vet1) || | ||
!this.adjList.has(vet2) || | ||
vet1 === vet2 | ||
) { | ||
throw new Error('Illegal Argument Exception'); | ||
} | ||
// 删除边 vet1 - vet2 | ||
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1); | ||
|
@@ -57,7 +65,7 @@ class GraphAdjList { | |
/* 删除顶点 */ | ||
removeVertex(vet) { | ||
if (!this.adjList.has(vet)) { | ||
throw new Error("Illegal Argument Exception"); | ||
throw new Error('Illegal Argument Exception'); | ||
} | ||
// 在邻接表中删除顶点 vet 对应的链表 | ||
this.adjList.delete(vet); | ||
|
@@ -72,13 +80,13 @@ class GraphAdjList { | |
|
||
/* 打印邻接表 */ | ||
print() { | ||
console.log("邻接表 ="); | ||
console.log('邻接表 ='); | ||
for (const [key, value] of this.adjList) { | ||
const tmp = []; | ||
for (const vertex of value) { | ||
tmp.push(vertex.val); | ||
} | ||
console.log(key.val + ": " + tmp.join()); | ||
console.log(key.val + ': ' + tmp.join()); | ||
} | ||
} | ||
} | ||
|
@@ -91,37 +99,43 @@ if (require.main === module) { | |
v2 = new Vertex(2), | ||
v3 = new Vertex(5), | ||
v4 = new Vertex(4); | ||
const edges = [[v0, v1], [v1, v2], [v2, v3], [v0, v3], [v2, v4], [v3, v4]]; | ||
const edges = [ | ||
[v0, v1], | ||
[v1, v2], | ||
[v2, v3], | ||
[v0, v3], | ||
[v2, v4], | ||
[v3, v4], | ||
]; | ||
const graph = new GraphAdjList(edges); | ||
console.log("\n初始化后,图为"); | ||
console.log('\n初始化后,图为'); | ||
graph.print(); | ||
|
||
/* 添加边 */ | ||
// 顶点 1, 2 即 v0, v2 | ||
graph.addEdge(v0, v2); | ||
console.log("\n添加边 1-2 后,图为"); | ||
console.log('\n添加边 1-2 后,图为'); | ||
graph.print(); | ||
|
||
/* 删除边 */ | ||
// 顶点 1, 3 即 v0, v1 | ||
graph.removeEdge(v0, v1); | ||
console.log("\n删除边 1-3 后,图为"); | ||
console.log('\n删除边 1-3 后,图为'); | ||
graph.print(); | ||
|
||
/* 添加顶点 */ | ||
const v5 = new Vertex(6); | ||
graph.addVertex(v5); | ||
console.log("\n添加顶点 6 后,图为"); | ||
console.log('\n添加顶点 6 后,图为'); | ||
graph.print(); | ||
|
||
/* 删除顶点 */ | ||
// 顶点 3 即 v1 | ||
graph.removeVertex(v1); | ||
console.log("\n删除顶点 3 后,图为"); | ||
console.log('\n删除顶点 3 后,图为'); | ||
graph.print(); | ||
|
||
} | ||
|
||
module.exports = { | ||
GraphAdjList | ||
GraphAdjList, | ||
}; |
Oops, something went wrong.