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,56 +5,56 @@
*/
/* 初始化列表 */
const list: number[] = [1, 3, 2, 5, 4];
console.log(`列表 list = ${list}`);
const nums: number[] = [1, 3, 2, 5, 4];
console.log(`列表 nums = ${nums}`);
/* 访问元素 */
const num: number = list[1];
const num: number = 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: number[] = [6, 8, 7, 10, 9];
list.push(...list1);
console.log(`将列表 list1 拼接到 list 之后,得到 list = ${list}`);
const nums1: number[] = [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}`);
export {};

View File

@@ -6,14 +6,14 @@
/* 列表类简易实现 */
class MyList {
private nums: Array<number>; // 数组(存储列表元素)
private arr: Array<number>; // 数组(存储列表元素)
private _capacity: number = 10; // 列表容量
private _size: number = 0; // 列表长度(即当前元素数量)
private extendRatio: number = 2; // 每次列表扩容的倍数
/* 构造方法 */
constructor() {
this.nums = new Array(this._capacity);
this.arr = new Array(this._capacity);
}
/* 获取列表长度(即当前元素数量)*/
@@ -30,13 +30,13 @@ class MyList {
public get(index: number): number {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= this._size) throw new Error('索引越界');
return this.nums[index];
return this.arr[index];
}
/* 更新元素 */
public set(index: number, num: number): void {
if (index < 0 || index >= this._size) throw new Error('索引越界');
this.nums[index] = num;
this.arr[index] = num;
}
/* 尾部添加元素 */
@@ -44,7 +44,7 @@ class MyList {
// 如果长度等于容量,则需要扩容
if (this._size === this._capacity) this.extendCapacity();
// 将新元素添加到列表尾部
this.nums[this._size] = num;
this.arr[this._size] = num;
this._size++;
}
@@ -57,20 +57,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++;
}
/* 删除元素 */
public remove(index: number): number {
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--;
@@ -81,61 +81,61 @@ class MyList {
/* 列表扩容 */
public extendCapacity(): void {
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
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;
}
/* 将列表转换为数组 */
public toArray(): number[] {
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()}`
);
export {};