Add the remain JavaScript code (Chapter of Array and Linkedlist)
This commit is contained in:
parent
80ef96da69
commit
f6939dab89
84
codes/javascript/chapter_array_and_linkedlist/linked_list.js
Normal file
84
codes/javascript/chapter_array_and_linkedlist/linked_list.js
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* File: linked_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const ListNode = require('../include/ListNode.js');
|
||||
const { printLinkedList } = require('../include/PrintUtil.js');
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
function insert(n0, P) {
|
||||
const n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
function remove(n0) {
|
||||
if (!n0.next) {
|
||||
return;
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化链表 */
|
||||
// 初始化各个结点
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log('初始化的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入结点 */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log('插入结点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 删除结点 */
|
||||
remove(n0);
|
||||
console.log('删除结点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 访问结点 */
|
||||
const node = access(n0, 3);
|
||||
console.log(`链表中索引 3 处的结点的值 = ${node?.val}`);
|
||||
|
||||
/* 查找结点 */
|
||||
const index = find(n0, 2);
|
||||
console.log(`链表中值为 2 的结点的索引 = ${index}`);
|
64
codes/javascript/chapter_array_and_linkedlist/list.js
Normal file
64
codes/javascript/chapter_array_and_linkedlist/list.js
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* File: list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* 初始化列表 */
|
||||
const list = [1, 3, 2, 5, 4];
|
||||
console.log(`列表 list = ${list}`);
|
||||
|
||||
/* 访问元素 */
|
||||
const num = list[1];
|
||||
console.log(`访问索引 1 处的元素,得到 num = ${num}`);
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list}`);
|
||||
|
||||
/* 清空列表 */
|
||||
list.length = 0;
|
||||
console.log(`清空列表后 list = ${list}`);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
console.log(`添加元素后 list = ${list}`);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.splice(3, 0, 6);
|
||||
console.log(`在索引 3 处插入数字 6 ,得到 list = ${list}`);
|
||||
|
||||
/* 删除元素 */
|
||||
list.splice(3, 1);
|
||||
console.log(`删除索引 3 处的元素,得到 list = ${list}`);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const n of list) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const n of list) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
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}`);
|
145
codes/javascript/chapter_array_and_linkedlist/my_list.js
Normal file
145
codes/javascript/chapter_array_and_linkedlist/my_list.js
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* File: my_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
#nums = new Array(); // 数组(存储列表元素)
|
||||
#capacity = 10; // 列表容量
|
||||
#size = 0; // 列表长度(即当前元素数量)
|
||||
#extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
constructor() {
|
||||
this.#nums = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
capacity() {
|
||||
return this.#capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
get(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;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
add(num) {
|
||||
// 如果长度等于容量,则需要扩容
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 将新元素添加到列表尾部
|
||||
this.#nums[this.#size] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
insert(index, num) {
|
||||
if (index >= this.#size) {
|
||||
throw new Error('索引越界');
|
||||
}
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (let j = this.#size - 1; j >= index; j--) {
|
||||
this.#nums[j + 1] = this.#nums[j];
|
||||
}
|
||||
// 更新元素数量
|
||||
this.#nums[index] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
remove(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];
|
||||
}
|
||||
// 更新元素数量
|
||||
this.#size--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
extendCapacity() {
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化列表 */
|
||||
let list = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
console.log(
|
||||
`列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}`
|
||||
);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
console.log(`在索引 3 处插入数字 6 ,得到 list = ${list.toArray()}`);
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
console.log(`删除索引 3 处的元素,得到 list = ${list.toArray()}`);
|
||||
|
||||
/* 访问元素 */
|
||||
let num = list.get(1);
|
||||
console.log(`访问索引 1 处的元素,得到 num = ${num}`);
|
||||
|
||||
/* 更新元素 */
|
||||
list.set(1, 0);
|
||||
console.log(`将索引 1 处的元素更新为 0 ,得到 list = ${list.toArray()}`);
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i);
|
||||
}
|
||||
console.log(
|
||||
`扩容后的列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}`
|
||||
);
|
17
codes/javascript/include/ListNode.js
Normal file
17
codes/javascript/include/ListNode.js
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* File: ListNode.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
class ListNode {
|
||||
constructor(val, next) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.next = next === undefined ? null : next;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ListNode;
|
Loading…
Reference in New Issue
Block a user