From 39fe7091c9979c28a815c0ef646710b7aa8638b1 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Fri, 19 Apr 2024 16:18:13 +0800 Subject: [PATCH] feat: add main --- .../chapter_graph/graph_adjacency_list.ts | 76 ++++++++++--------- codes/typescript/chapter_heap/my_heap.ts | 4 +- codes/typescript/package-lock.json | 16 ++++ codes/typescript/package.json | 1 + codes/typescript/tsconfig.json | 2 +- 5 files changed, 59 insertions(+), 40 deletions(-) diff --git a/codes/typescript/chapter_graph/graph_adjacency_list.ts b/codes/typescript/chapter_graph/graph_adjacency_list.ts index 33c1f31284..439efef211 100644 --- a/codes/typescript/chapter_graph/graph_adjacency_list.ts +++ b/codes/typescript/chapter_graph/graph_adjacency_list.ts @@ -92,46 +92,48 @@ class GraphAdjList { } /* Driver Code */ -/* 初始化无向图 */ -const v0 = new Vertex(1), - v1 = new Vertex(3), - 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 graph = new GraphAdjList(edges); -console.log('\n初始化后,图为'); -graph.print(); +if (import.meta.url === new URL('file://' + process.argv[1]).href) { + /* 初始化无向图 */ + const v0 = new Vertex(1), + v1 = new Vertex(3), + 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 graph = new GraphAdjList(edges); + console.log('\n初始化后,图为'); + graph.print(); -/* 添加边 */ -// 顶点 1, 2 即 v0, v2 -graph.addEdge(v0, v2); -console.log('\n添加边 1-2 后,图为'); -graph.print(); + /* 添加边 */ + // 顶点 1, 2 即 v0, v2 + graph.addEdge(v0, v2); + console.log('\n添加边 1-2 后,图为'); + graph.print(); -/* 删除边 */ -// 顶点 1, 3 即 v0, v1 -graph.removeEdge(v0, v1); -console.log('\n删除边 1-3 后,图为'); -graph.print(); + /* 删除边 */ + // 顶点 1, 3 即 v0, v1 + graph.removeEdge(v0, v1); + console.log('\n删除边 1-3 后,图为'); + graph.print(); -/* 添加顶点 */ -const v5 = new Vertex(6); -graph.addVertex(v5); -console.log('\n添加顶点 6 后,图为'); -graph.print(); + /* 添加顶点 */ + const v5 = new Vertex(6); + graph.addVertex(v5); + console.log('\n添加顶点 6 后,图为'); + graph.print(); -/* 删除顶点 */ -// 顶点 3 即 v1 -graph.removeVertex(v1); -console.log('\n删除顶点 3 后,图为'); -graph.print(); + /* 删除顶点 */ + // 顶点 3 即 v1 + graph.removeVertex(v1); + console.log('\n删除顶点 3 后,图为'); + graph.print(); +} export { GraphAdjList }; diff --git a/codes/typescript/chapter_heap/my_heap.ts b/codes/typescript/chapter_heap/my_heap.ts index a134cc2e08..d225e126cc 100644 --- a/codes/typescript/chapter_heap/my_heap.ts +++ b/codes/typescript/chapter_heap/my_heap.ts @@ -122,7 +122,7 @@ class MaxHeap { } /* Driver Code */ - +if (import.meta.url === new URL('file://' + process.argv[1]).href) { /* 初始化大顶堆 */ const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]); console.log('\n输入列表并建堆后'); @@ -150,6 +150,6 @@ class MaxHeap { /* 判断堆是否为空 */ const isEmpty = maxHeap.isEmpty(); console.log(`\n堆是否为空 ${isEmpty}`); - +} export { MaxHeap }; diff --git a/codes/typescript/package-lock.json b/codes/typescript/package-lock.json index e6bcd0dfa7..235377840b 100644 --- a/codes/typescript/package-lock.json +++ b/codes/typescript/package-lock.json @@ -5,6 +5,7 @@ "packages": { "": { "devDependencies": { + "@types/node": "^20.12.7", "tsx": "^4.7.2", "typescript": "^5.4.5" } @@ -25,6 +26,15 @@ "node": ">=12" } }, + "node_modules/@types/node": { + "version": "20.12.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", + "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, "node_modules/esbuild": { "version": "0.19.12", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.19.12.tgz", @@ -115,6 +125,12 @@ "engines": { "node": ">=14.17" } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true } } } diff --git a/codes/typescript/package.json b/codes/typescript/package.json index b0bc3d1889..ad9d39dd35 100644 --- a/codes/typescript/package.json +++ b/codes/typescript/package.json @@ -6,6 +6,7 @@ "check": "tsc" }, "devDependencies": { + "@types/node": "^20.12.7", "tsx": "^4.7.2", "typescript": "^5.4.5" } diff --git a/codes/typescript/tsconfig.json b/codes/typescript/tsconfig.json index 2658029481..20311b7003 100644 --- a/codes/typescript/tsconfig.json +++ b/codes/typescript/tsconfig.json @@ -10,7 +10,7 @@ "moduleResolution": "node", "paths": {}, "resolveJsonModule": true, - "types": [], + "types": ["@types/node"], "noEmit": true,