Update variable names in list and my_list

This commit is contained in:
krahets
2023-10-09 18:20:42 +08:00
parent e5f8c93f5d
commit fb552987f5
25 changed files with 966 additions and 966 deletions

View File

@ -5,54 +5,54 @@
*/
/* 初始化列表 */
const list = [1, 3, 2, 5, 4];
console.log(`列表 list = ${list}`);
const nums = [1, 3, 2, 5, 4];
console.log(`列表 nums = ${nums}`);
/* 访问元素 */
const num = list[1];
const num = nums[1];
console.log(`访问索引 1 处的元素,得到 num = ${num}`);
/* 更新元素 */
list[1] = 0;
console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list}`);
nums[1] = 0;
console.log(`将索引 1 处的元素更新为 0 ,得到 nums = ${nums}`);
/* 清空列表 */
list.length = 0;
console.log(`清空列表后 list = ${list}`);
nums.length = 0;
console.log(`清空列表后 nums = ${nums}`);
/* 尾部添加元素 */
list.push(1);
list.push(3);
list.push(2);
list.push(5);
list.push(4);
console.log(`添加元素后 list = ${list}`);
nums.push(1);
nums.push(3);
nums.push(2);
nums.push(5);
nums.push(4);
console.log(`添加元素后 nums = ${nums}`);
/* 中间插入元素 */
list.splice(3, 0, 6);
console.log(`在索引 3 处插入数字 6 ,得到 list = ${list}`);
nums.splice(3, 0, 6);
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums}`);
/* 删除元素 */
list.splice(3, 1);
console.log(`删除索引 3 处的元素,得到 list = ${list}`);
nums.splice(3, 1);
console.log(`删除索引 3 处的元素,得到 nums = ${nums}`);
/* 通过索引遍历列表 */
let count = 0;
for (let i = 0; i < list.length; i++) {
for (let i = 0; i < nums.length; i++) {
count++;
}
/* 直接遍历列表元素 */
count = 0;
for (const n of list) {
for (const n of nums) {
count++;
}
/* 拼接两个列表 */
const list1 = [6, 8, 7, 10, 9];
list.push(...list1);
console.log(`将列表 list1 拼接到 list 之后,得到 list = ${list}`);
const nums1 = [6, 8, 7, 10, 9];
nums.push(...nums1);
console.log(`将列表 nums1 拼接到 nums 之后,得到 nums = ${nums}`);
/* 排序列表 */
list.sort((a, b) => a - b);
console.log(`排序列表后 list = ${list}`);
nums.sort((a, b) => a - b);
console.log(`排序列表后 nums = ${nums}`);

View File

@ -6,14 +6,14 @@
/* 列表类简易实现 */
class MyList {
#nums = new Array(); // 数组(存储列表元素)
#arr = new Array(); // 数组(存储列表元素)
#capacity = 10; // 列表容量
#size = 0; // 列表长度(即当前元素数量)
#extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
constructor() {
this.#nums = new Array(this.#capacity);
this.#arr = new Array(this.#capacity);
}
/* 获取列表长度(即当前元素数量)*/
@ -30,13 +30,13 @@ class MyList {
get(index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= this.#size) throw new Error('索引越界');
return this.#nums[index];
return this.#arr[index];
}
/* 更新元素 */
set(index, num) {
if (index < 0 || index >= this.#size) throw new Error('索引越界');
this.#nums[index] = num;
this.#arr[index] = num;
}
/* 尾部添加元素 */
@ -46,7 +46,7 @@ class MyList {
this.extendCapacity();
}
// 将新元素添加到列表尾部
this.#nums[this.#size] = num;
this.#arr[this.#size] = num;
this.#size++;
}
@ -59,20 +59,20 @@ class MyList {
}
// 将索引 index 以及之后的元素都向后移动一位
for (let j = this.#size - 1; j >= index; j--) {
this.#nums[j + 1] = this.#nums[j];
this.#arr[j + 1] = this.#arr[j];
}
// 更新元素数量
this.#nums[index] = num;
this.#arr[index] = num;
this.#size++;
}
/* 删除元素 */
remove(index) {
if (index < 0 || index >= this.#size) throw new Error('索引越界');
let num = this.#nums[index];
let num = this.#arr[index];
// 将索引 index 之后的元素都向前移动一位
for (let j = index; j < this.#size - 1; j++) {
this.#nums[j] = this.#nums[j + 1];
this.#arr[j] = this.#arr[j + 1];
}
// 更新元素数量
this.#size--;
@ -83,59 +83,59 @@ class MyList {
/* 列表扩容 */
extendCapacity() {
// 新建一个长度为原数组 extendRatio 倍的新数组,并将原数组拷贝到新数组
this.#nums = this.#nums.concat(
this.#arr = this.#arr.concat(
new Array(this.capacity() * (this.#extendRatio - 1))
);
// 更新列表容量
this.#capacity = this.#nums.length;
this.#capacity = this.#arr.length;
}
/* 将列表转换为数组 */
toArray() {
let size = this.size();
// 仅转换有效长度范围内的列表元素
const nums = new Array(size);
const arr = new Array(size);
for (let i = 0; i < size; i++) {
nums[i] = this.get(i);
arr[i] = this.get(i);
}
return nums;
return arr;
}
}
/* Driver Code */
/* 初始化列表 */
const list = new MyList();
const nums = new MyList();
/* 尾部添加元素 */
list.add(1);
list.add(3);
list.add(2);
list.add(5);
list.add(4);
nums.add(1);
nums.add(3);
nums.add(2);
nums.add(5);
nums.add(4);
console.log(
`列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}`
`列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}`
);
/* 中间插入元素 */
list.insert(3, 6);
console.log(`在索引 3 处插入数字 6 ,得到 list = ${list.toArray()}`);
nums.insert(3, 6);
console.log(`在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray()}`);
/* 删除元素 */
list.remove(3);
console.log(`删除索引 3 处的元素,得到 list = ${list.toArray()}`);
nums.remove(3);
console.log(`删除索引 3 处的元素,得到 nums = ${nums.toArray()}`);
/* 访问元素 */
const num = list.get(1);
const num = nums.get(1);
console.log(`访问索引 1 处的元素,得到 num = ${num}`);
/* 更新元素 */
list.set(1, 0);
console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list.toArray()}`);
nums.set(1, 0);
console.log(`将索引 1 处的元素更新为 0 ,得到 nums = ${nums.toArray()}`);
/* 测试扩容机制 */
for (let i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
list.add(i);
nums.add(i);
}
console.log(
`扩容后的列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}`
`扩容后的列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}`
);