diff --git a/codes/javascript/chapter_array_and_linkedlist/array.js b/codes/javascript/chapter_array_and_linkedlist/array.js index 5e24d322d..754e8fb15 100644 --- a/codes/javascript/chapter_array_and_linkedlist/array.js +++ b/codes/javascript/chapter_array_and_linkedlist/array.js @@ -5,30 +5,30 @@ */ /* 随机访问元素 */ -function randomAccess(nums){ +function randomAccess(nums) { // 在区间 [0, nums.length) 中随机抽取一个数字 const random_index = Math.floor(Math.random() * nums.length); // 获取并返回随机元素 - random_num = nums[random_index]; + const random_num = nums[random_index]; return random_num; } /* 扩展数组长度 */ -// 请注意,Python 的 list 是动态数组,可以直接扩展 -// 为了方便学习,本函数将 list 看作是长度不可变的数组 -function extend(nums, enlarge){ - // 初始化一个扩展长度后的数组 - let res = new Array(nums.length + enlarge).fill(0); - // 将原数组中的所有元素复制到新数组 - for(let i=0; i= index; i--) { nums[i] = nums[i - 1]; @@ -38,45 +38,43 @@ function insert(nums, num, index){ } /* 删除索引 index 处元素 */ -function remove(nums, index){ +function remove(nums, index) { // 把索引 index 之后的所有元素向前移动一位 for (let i = index; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; - } + } } - + /* 遍历数组 */ -function traverse(nums){ +function traverse(nums) { let count = 0; // 通过索引遍历数组 for (let i = 0; i < nums.length; i++) { - count++; + count++; } // 直接遍历数组 - for(let num of nums){ - count += 1; - } + for (let num of nums) { + count += 1; + } } /* 在数组中查找指定元素 */ -function find(nums, target){ +function find(nums, target) { for (let i = 0; i < nums.length; i++) { - if (nums[i] === target) - return i; + if (nums[i] == target) return i; } return -1; } - /* Driver Codes*/ /* 初始化数组 */ -var arr = new Array(5).fill(0); +const arr = new Array(5).fill(0); console.log("数组 arr =", arr); -var nums = [1, 3, 2, 5, 4]; +let nums = [1, 3, 2, 5, 4]; console.log("数组 nums =", nums); /* 随机访问 */ -random_num = randomAccess(nums); +let random_num = randomAccess(nums); console.log("在 nums 中获取随机元素", random_num); /* 长度扩展 */ @@ -95,5 +93,5 @@ console.log("删除索引 2 处的元素,得到 nums =", nums); traverse(nums); /* 查找元素 */ -var index = find(nums, 3); +let index = find(nums, 3); console.log("在 nums 中查找元素 3 ,得到索引 =", index); diff --git a/codes/javascript/chapter_array_and_linkedlist/linked_list.js b/codes/javascript/chapter_array_and_linkedlist/linked_list.js index 60201a812..cf701a0c0 100644 --- a/codes/javascript/chapter_array_and_linkedlist/linked_list.js +++ b/codes/javascript/chapter_array_and_linkedlist/linked_list.js @@ -1,7 +1,7 @@ /** * File: linked_list.js * Created Time: 2022-12-12 - * Author: IsChristina (christinaxia77@foxmail.com) + * Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com) */ const PrintUtil = require("../include/PrintUtil"); @@ -9,7 +9,7 @@ const ListNode = require("../include/ListNode"); /* 在链表的结点 n0 之后插入结点 P */ function insert(n0, P) { - let n1 = n0.next; + const n1 = n0.next; n0.next = P; P.next = n1; } @@ -19,17 +19,18 @@ function remove(n0) { if (!n0.next) return; // n0 -> P -> n1 - let P = n0.next; - let n1 = P.next; + const P = n0.next; + const n1 = P.next; n0.next = n1; } /* 访问链表中索引为 index 的结点 */ function access(head, index) { for (let i = 0; i < index; i++) { - head = head.next; - if (!head) + if (!head) { return null; + } + head = head.next; } return head; } @@ -38,18 +39,18 @@ function access(head, index) { function find(head, target) { let index = 0; while (head !== null) { - if (head.val === target) - return index; + if (head.val === target) { + return index; + } head = head.next; - index++; + index += 1; } return -1; } /* Driver Code */ - /* 初始化链表 */ -// 初始化各个结点 +// 初始化各个结点 const n0 = new ListNode(1); const n1 = new ListNode(3); const n2 = new ListNode(2); @@ -74,11 +75,9 @@ console.log("删除结点后的链表为"); PrintUtil.printLinkedList(n0); /* 访问结点 */ -var node = access(n0, 3); +const node = access(n0, 3); console.log("链表中索引 3 处的结点的值 = " + node.val); /* 查找结点 */ -var index = find(n0, 2); +const index = find(n0, 2); console.log("链表中值为 2 的结点的索引 = " + index); - - diff --git a/codes/javascript/chapter_array_and_linkedlist/list.js b/codes/javascript/chapter_array_and_linkedlist/list.js index 2c2c32b7f..19318337f 100644 --- a/codes/javascript/chapter_array_and_linkedlist/list.js +++ b/codes/javascript/chapter_array_and_linkedlist/list.js @@ -1,24 +1,24 @@ -/** +/* * File: list.js * Created Time: 2022-12-12 - * Author: IsChristina (christinaxia77@foxmail.com) + * Author: Justin (xiefahit@gmail.com) */ /* 初始化列表 */ -var list = [1, 3, 2, 5, 4]; -console.log("列表 list = " + list); +const list = [1, 3, 2, 5, 4]; +console.log(`列表 list = ${list}`); /* 访问元素 */ -var num = list[1]; -console.log("访问索引 1 处的元素,得到 num = " + num); +const num = list[1]; +console.log(`访问索引 1 处的元素,得到 num = ${num}`); /* 更新元素 */ list[1] = 0; -console.log("将索引 1 处的元素更新为 0 ,得到 list = " + list); +console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list}`); /* 清空列表 */ -list = []; -console.log("清空列表后 list = " + list); +list.length = 0; +console.log(`清空列表后 list = ${list}`); /* 尾部添加元素 */ list.push(1); @@ -26,18 +26,18 @@ list.push(3); list.push(2); list.push(5); list.push(4); -console.log("添加元素后 list = " + list); +console.log(`添加元素后 list = ${list}`); /* 中间插入元素 */ list.splice(3, 0, 6); -console.log("在索引 3 处插入数字 6 ,得到 list = " + list); +console.log(`在索引 3 处插入数字 6 ,得到 list = ${list}`); /* 删除元素 */ list.splice(3, 1); -console.log("删除索引 3 处的元素,得到 list = " + list); +console.log(`删除索引 3 处的元素,得到 list = ${list}`); /* 通过索引遍历列表 */ -var count = 0; +let count = 0; for (let i = 0; i < list.length; i++) { count++; } @@ -49,11 +49,10 @@ for (const n of list) { } /* 拼接两个列表 */ -var list1 = [ 6, 8, 7, 10, 9 ]; -list.push(...list1) -console.log("将列表 list1 拼接到 list 之后,得到 list = " + list); +const list1 = [6, 8, 7, 10, 9]; +list.push(...list1); +console.log(`将列表 list1 拼接到 list 之后,得到 list = ${list}`); /* 排序列表 */ list.sort((a, b) => a - b); -console.log("排序列表后 list = " + list); - +console.log(`排序列表后 list = ${list}`); diff --git a/codes/javascript/chapter_array_and_linkedlist/my_list.js b/codes/javascript/chapter_array_and_linkedlist/my_list.js index 0b4066db9..a861b1f0d 100644 --- a/codes/javascript/chapter_array_and_linkedlist/my_list.js +++ b/codes/javascript/chapter_array_and_linkedlist/my_list.js @@ -1,83 +1,85 @@ -/** +/* * File: my_list.js * Created Time: 2022-12-12 - * Author: IsChristina (christinaxia77@foxmail.com) + * Author: Justin (xiefahit@gmail.com) */ /* 列表类简易实现 */ class MyList { - nums; // 数组(存储列表元素) - _capacity = 10; // 列表容量 - _size = 0; // 列表长度(即当前元素数量) - extendRatio = 2; // 每次列表扩容的倍数 + #nums = new Array(); // 数组(存储列表元素) + #capacity = 10; // 列表容量 + #size = 0; // 列表长度(即当前元素数量) + #extendRatio = 2; // 每次列表扩容的倍数 /* 构造函数 */ constructor() { - this.nums = new Array(this._capacity); + this.#nums = new Array(this.#capacity); } /* 获取列表长度(即当前元素数量)*/ - get size() { - return this._size; + size() { + return this.#size; } /* 获取列表容量 */ - get capacity() { - return this._capacity; + capacity() { + return this.#capacity; } /* 访问元素 */ get(index) { // 索引如果越界则抛出异常,下同 - if (index >= this._size) - throw new Error("索引越界"); - return this.nums[index]; + if (index >= this.#size) { + throw new Error('索引越界'); + } + return this.#nums[index]; } /* 更新元素 */ set(index, num) { - if (index >= this._size) - throw new Error("索引越界"); - this.nums[index] = num; + if (index >= this._size) throw new Error('索引越界'); + this.#nums[index] = num; } /* 尾部添加元素 */ add(num) { - //元素数量超出容量时,触发扩容机制 - if (this._size === this._capacity) + // 如果长度等于容量,则需要扩容 + if (this.#size === this.#capacity) { this.extendCapacity(); - this.nums[this._size] = num; - // 更新元素数量 - this._size++; + } + // 将新元素添加到列表尾部 + this.#nums[this.#size] = num; + this.#size++; } /* 中间插入元素 */ insert(index, num) { - if (index >= this._size) - throw new Error("索引越界"); + if (index >= this.#size) { + throw new Error('索引越界'); + } // 元素数量超出容量时,触发扩容机制 - if (this._size === this._capacity) + if (this.#size === this.#capacity) { this.extendCapacity(); + } // 将索引 index 以及之后的元素都向后移动一位 - for (let j = this._size - 1; j >= index; j--) { - this.nums[j + 1] = this.nums[j]; + for (let j = this.#size - 1; j >= index; j--) { + this.#nums[j + 1] = this.#nums[j]; } - this.nums[index] = num; // 更新元素数量 - this._size++; + this.#nums[index] = num; + this.#size++; } /* 删除元素 */ remove(index) { - if (index >= this._size) - throw new Error("索引越界"); - let num = this.nums[index]; + if (index >= this.#size) throw new Error('索引越界'); + let num = this.#nums[index]; // 将索引 index 之后的元素都向前移动一位 - for (let j = index; j < this._size - 1; j++) { - this.nums[j] = this.nums[j + 1]; + for (let j = index; j < this.#size - 1; j++) { + this.#nums[j] = this.#nums[j + 1]; } // 更新元素数量 - this._size--; + this.#size--; // 返回被删除元素 return num; } @@ -85,18 +87,18 @@ class MyList { /* 列表扩容 */ extendCapacity() { // 新建一个长度为 size 的数组,并将原数组拷贝到新数组 - this.nums = this.nums.concat( - new Array(this.capacity * (this.extendRatio - 1)) + this.#nums = this.#nums.concat( + new Array(this.capacity() * (this.#extendRatio - 1)) ); // 更新列表容量 - this._capacity = this.nums.length; + this.#capacity = this.#nums.length; } /* 将列表转换为数组 */ toArray() { - let size = this.size; + let size = this.size(); // 仅转换有效长度范围内的列表元素 - let nums = new Array(size); + const nums = new Array(size); for (let i = 0; i < size; i++) { nums[i] = this.get(i); } @@ -105,39 +107,39 @@ class MyList { } /* Driver Code */ - /* 初始化列表 */ -var list = new MyList(); +const list = new MyList(); /* 尾部添加元素 */ list.add(1); list.add(3); list.add(2); list.add(5); list.add(4); -console.log("列表 list = " + list.toArray().toString() + - " ,容量 = " + list.capacity + " ,长度 = " + list.size); +console.log( + `列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}` +); /* 中间插入元素 */ list.insert(3, 6); -console.log("在索引 3 处插入数字 6 ,得到 list = " + list.toArray().toString()); +console.log(`在索引 3 处插入数字 6 ,得到 list = ${list.toArray()}`); /* 删除元素 */ list.remove(3); -console.log("删除索引 3 处的元素,得到 list = " + list.toArray().toString()); +console.log(`删除索引 3 处的元素,得到 list = ${list.toArray()}`); /* 访问元素 */ -var num = list.get(1); -console.log("访问索引 1 处的元素,得到 num = " + num); +const num = list.get(1); +console.log(`访问索引 1 处的元素,得到 num = ${num}`); /* 更新元素 */ list.set(1, 0); -console.log("将索引 1 处的元素更新为 0 ,得到 list = " + list.toArray().toString()); +console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list.toArray()}`); /* 测试扩容机制 */ for (let i = 0; i < 10; i++) { // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 list.add(i); } -console.log("扩容后的列表 list = " + list.toArray().toString() + - " ,容量 = " + list.capacity + " ,长度 = " + list.size); - +console.log( + `扩容后的列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}` +); diff --git a/codes/javascript/include/ListNode.js b/codes/javascript/include/ListNode.js index 793915ec3..72306548c 100755 --- a/codes/javascript/include/ListNode.js +++ b/codes/javascript/include/ListNode.js @@ -21,8 +21,8 @@ class ListNode { * @return */ arrToLinkedList(arr) { - let dum = new ListNode(0); - let head = dum; + const dum = new ListNode(0); + const head = dum; for (const val of arr) { head.next = new ListNode(val); head = head.next; diff --git a/codes/typescript/chapter_array_and_linkedlist/linked_list.ts b/codes/typescript/chapter_array_and_linkedlist/linked_list.ts index d2c339df0..484322581 100644 --- a/codes/typescript/chapter_array_and_linkedlist/linked_list.ts +++ b/codes/typescript/chapter_array_and_linkedlist/linked_list.ts @@ -7,12 +7,14 @@ import ListNode from '../module/ListNode'; import { printLinkedList } from '../module/PrintUtil'; +/* 在链表的结点 n0 之后插入结点 P */ function insert(n0: ListNode, P: ListNode): void { const n1 = n0.next; n0.next = P; P.next = n1; } +/* 删除链表的结点 n0 之后的首个结点 */ function remove(n0: ListNode): void { if (!n0.next) { return; @@ -23,6 +25,7 @@ function remove(n0: ListNode): void { n0.next = n1; } +/* 访问链表中索引为 index 的结点 */ function access(head: ListNode | null, index: number): ListNode | null { for (let i = 0; i < index; i++) { if (!head) { @@ -33,6 +36,7 @@ function access(head: ListNode | null, index: number): ListNode | null { return head; } +/* 在链表中查找值为 target 的首个结点 */ function find(head: ListNode | null, target: number): number { let index = 0; while (head !== null) { diff --git a/codes/typescript/module/ListNode.ts b/codes/typescript/module/ListNode.ts index 543f1cf3a..d6d60616d 100644 --- a/codes/typescript/module/ListNode.ts +++ b/codes/typescript/module/ListNode.ts @@ -1,4 +1,12 @@ -// Definition for singly-linked list. +/* + * File: ListNode.ts + * Created Time: 2022-12-10 + * Author: Justin (xiefahit@gmail.com) + */ + +/** + * Definition for a singly-linked list node + */ export default class ListNode { val: number; next: ListNode | null; diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 917c75aac..caa7145b0 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -58,8 +58,8 @@ comments: true ```typescript title="array.ts" /* 初始化数组 */ - let arr: number[] = new Array(5).fill(0) - let nums: number[] = [1, 3, 2, 5, 4] + let arr: number[] = new Array(5).fill(0); + let nums: number[] = [1, 3, 2, 5, 4]; ``` === "C" @@ -138,11 +138,11 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```javascript title="array.js" /* 随机返回一个数组元素 */ - function randomAccess(nums){ + function randomAccess(nums) { // 在区间 [0, nums.length) 中随机抽取一个数字 const random_index = Math.floor(Math.random() * nums.length); // 获取并返回随机元素 - random_num = nums[random_index]; + const random_num = nums[random_index]; return random_num; } ``` @@ -153,10 +153,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex /* 随机返回一个数组元素 */ function randomAccess(nums: number[]): number { // 在区间 [0, nums.length) 中随机抽取一个数字 - const random_index = Math.floor(Math.random() * nums.length) + const random_index = Math.floor(Math.random() * nums.length); // 获取并返回随机元素 - const random_num = nums[random_index] - return random_num + const random_num = nums[random_index]; + return random_num; } ``` @@ -234,12 +234,12 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```javascript title="array.js" /* 扩展数组长度 */ - function extend(nums, enlarge){ + function extend(nums, enlarge) { // 初始化一个扩展长度后的数组 - let res = new Array(nums.length + enlarge).fill(0); + const res = new Array(nums.length + enlarge).fill(0); // 将原数组中的所有元素复制到新数组 - for(let i=0; i= index; i--) { nums[i] = nums[i - 1]; @@ -364,9 +364,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex // 将 num 赋给 index 处元素 nums[index] = num; } - + /* 删除索引 index 处元素 */ - function remove(nums, index){ + function remove(nums, index) { // 把索引 index 之后的所有元素向前移动一位 for (let i = index; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; @@ -381,17 +381,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex function insert(nums: number[], num: number, index: number): void { // 把索引 index 以及之后的所有元素向后移动一位 for (let i = nums.length - 1; i >= index; i--) { - nums[i] = nums[i - 1] + nums[i] = nums[i - 1]; } // 将 num 赋给 index 处元素 - nums[index] = num + nums[index] = num; } /* 删除索引 index 处元素 */ function remove(nums: number[], index: number): void { // 把索引 index 之后的所有元素向前移动一位 for (let i = index; i < nums.length - 1; i++) { - nums[i] = nums[i + 1] + nums[i] = nums[i + 1]; } } ``` @@ -466,14 +466,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```javascript title="array.js" /* 遍历数组 */ - function traverse(nums){ + function traverse(nums) { let count = 0; // 通过索引遍历数组 for (let i = 0; i < nums.length; i++) { count++; } // 直接遍历数组 - for(let num of nums){ + for (let num of nums) { count += 1; } } @@ -484,14 +484,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```typescript title="array.ts" /* 遍历数组 */ function traverse(nums: number[]): void { - let count = 0 + let count = 0; // 通过索引遍历数组 for (let i = 0; i < nums.length; i++) { - count++ + count++; } // 直接遍历数组 for(let num of nums){ - count += 1 + count += 1; } } ``` @@ -557,10 +557,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```javascript title="array.js" /* 在数组中查找指定元素 */ - function find(nums, target){ + function find(nums, target) { for (let i = 0; i < nums.length; i++) { - if (nums[i] == target) - return i; + if (nums[i] == target) return i; } return -1; } @@ -573,10 +572,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex function find(nums: number[], target: number): number { for (let i = 0; i < nums.length; i++) { if (nums[i] === target) { - return i + return i; } } - return -1 + return -1; } ``` diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 629fcecf2..7ebdbd7e0 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -163,7 +163,7 @@ comments: true ```js title="" /* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */ - // 初始化各个结点 + // 初始化各个结点 const n0 = new ListNode(1); const n1 = new ListNode(3); const n2 = new ListNode(2); @@ -390,9 +390,9 @@ comments: true /* 访问链表中索引为 index 的结点 */ function access(head, index) { for (let i = 0; i < index; i++) { - head = head.next; if (!head) return null; + head = head.next; } return head; } @@ -490,10 +490,11 @@ comments: true function find(head, target) { let index = 0; while (head !== null) { - if (head.val === target) - return index; + if (head.val === target) { + return index; + } head = head.next; - index++; + index += 1; } return -1; } @@ -601,7 +602,7 @@ comments: true val: number; next: ListNode | null; prev: ListNode | null; - constructor(val?: number, next?: ListNode | null) { + constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) { this.val = val === undefined ? 0 : val; // 结点值 this.next = next === undefined ? null : next; // 指向后继结点的引用 this.prev = prev === undefined ? null : prev; // 指向前驱结点的引用 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 23c1af95a..c2631048e 100644 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -44,7 +44,8 @@ comments: true === "JavaScript" ```js title="list.js" - var list = [1, 3, 2, 5, 4]; + /* 初始化列表 */ + const list = [1, 3, 2, 5, 4]; ``` === "TypeScript" @@ -108,7 +109,7 @@ comments: true ```js title="list.js" /* 访问元素 */ - var num = list[1]; + const num = list[1]; /* 更新元素 */ list[1] = 0; @@ -208,7 +209,7 @@ comments: true ```js title="list.js" /* 清空列表 */ - list = []; + list.length = 0; /* 尾部添加元素 */ list.push(1); @@ -314,7 +315,7 @@ comments: true ```js title="list.js" /* 通过索引遍历列表 */ - var count = 0; + let count = 0; for (let i = 0; i < list.length; i++) { count++; } @@ -391,8 +392,8 @@ comments: true ```js title="list.js" /* 拼接两个列表 */ - var list1 = [ 6, 8, 7, 10, 9 ]; - list.push(...list1) + const list1 = [6, 8, 7, 10, 9]; + list.push(...list1); ``` === "TypeScript" @@ -447,8 +448,8 @@ comments: true === "JavaScript" ```js title="list.js" - /* 排序列表 */ - list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列 + /* 排序列表 */ + list.sort((a, b) => a - b); ``` === "TypeScript" @@ -746,78 +747,80 @@ comments: true ```js title="my_list.js" /* 列表类简易实现 */ class MyList { - nums; // 数组(存储列表元素) - _capacity = 10; // 列表容量 - _size = 0; // 列表长度(即当前元素数量) - extendRatio = 2; // 每次列表扩容的倍数 + #nums = new Array(); // 数组(存储列表元素) + #capacity = 10; // 列表容量 + #size = 0; // 列表长度(即当前元素数量) + #extendRatio = 2; // 每次列表扩容的倍数 /* 构造函数 */ constructor() { - this.nums = new Array(this._capacity); + this.#nums = new Array(this.#capacity); } /* 获取列表长度(即当前元素数量)*/ - get size() { - return this._size; + size() { + return this.#size; } /* 获取列表容量 */ - get capacity() { - return this._capacity; + capacity() { + return this.#capacity; } /* 访问元素 */ get(index) { // 索引如果越界则抛出异常,下同 - if (index >= this._size) - throw new Error("索引越界"); - return this.nums[index]; + if (index >= this.#size) { + throw new Error('索引越界'); + } + return this.#nums[index]; } /* 更新元素 */ set(index, num) { - if (index >= this._size) - throw new Error("索引越界"); - this.nums[index] = num; + if (index >= this._size) throw new Error('索引越界'); + this.#nums[index] = num; } /* 尾部添加元素 */ add(num) { - //元素数量超出容量时,触发扩容机制 - if (this._size === this._capacity) + // 如果长度等于容量,则需要扩容 + if (this.#size === this.#capacity) { this.extendCapacity(); - this.nums[this._size] = num; - // 更新元素数量 - this._size++; + } + // 将新元素添加到列表尾部 + this.#nums[this.#size] = num; + this.#size++; } /* 中间插入元素 */ insert(index, num) { - if (index >= this._size) - throw new Error("索引越界"); + if (index >= this.#size) { + throw new Error('索引越界'); + } // 元素数量超出容量时,触发扩容机制 - if (this._size === this._capacity) + if (this.#size === this.#capacity) { this.extendCapacity(); + } // 将索引 index 以及之后的元素都向后移动一位 - for (let j = this._size - 1; j >= index; j--) { - this.nums[j + 1] = this.nums[j]; + for (let j = this.#size - 1; j >= index; j--) { + this.#nums[j + 1] = this.#nums[j]; } - this.nums[index] = num; // 更新元素数量 - this._size++; + this.#nums[index] = num; + this.#size++; } /* 删除元素 */ remove(index) { - if (index >= this._size) - throw new Error("索引越界"); - let num = this.nums[index]; + if (index >= this.#size) throw new Error('索引越界'); + let num = this.#nums[index]; // 将索引 index 之后的元素都向前移动一位 - for (let j = index; j < this._size - 1; j++) { - this.nums[j] = this.nums[j + 1]; + for (let j = index; j < this.#size - 1; j++) { + this.#nums[j] = this.#nums[j + 1]; } // 更新元素数量 - this._size--; + this.#size--; // 返回被删除元素 return num; } @@ -825,22 +828,11 @@ comments: true /* 列表扩容 */ extendCapacity() { // 新建一个长度为 size 的数组,并将原数组拷贝到新数组 - this.nums = this.nums.concat( - new Array(this.capacity * (this.extendRatio - 1)) + this.#nums = this.#nums.concat( + new Array(this.capacity() * (this.#extendRatio - 1)) ); // 更新列表容量 - this._capacity = this.nums.length; - } - - /* 将列表转换为数组 */ - toArray() { - let size = this.size; - // 仅转换有效长度范围内的列表元素 - let nums = new Array(size); - for (let i = 0; i < size; i++) { - nums[i] = this.get(i); - } - return nums; + this.#capacity = this.#nums.length; } } ```