add remaining code for Array and LinkedList
This commit is contained in:
@@ -57,7 +57,15 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
|
||||
/* 链表结点结构体 */
|
||||
class ListNode {
|
||||
val;
|
||||
next;
|
||||
constructor(val, next) {
|
||||
this.val = (val === undefined ? 0 : val); // 结点值
|
||||
this.next = (next === undefined ? null : next); // 指向下一结点的引用
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -154,7 +162,18 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
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;
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -264,7 +283,22 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
function insert(n0, P) {
|
||||
let n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
function remove(n0) {
|
||||
if (!n0.next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
let P = n0.next;
|
||||
let n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -353,7 +387,15 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
head = head.next;
|
||||
if (!head)
|
||||
return null;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -444,7 +486,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -528,7 +580,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title=""
|
||||
|
||||
/* 双向链表结点类 */
|
||||
class ListNode {
|
||||
val;
|
||||
next;
|
||||
prev;
|
||||
constructor(val, next) {
|
||||
this.val = val === undefined ? 0 : val; // 结点值
|
||||
this.next = next === undefined ? null : next; // 指向后继结点的引用
|
||||
this.prev = prev === undefined ? null : prev; // 指向前驱结点的引用
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
@@ -44,7 +44,7 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
|
||||
var list = [1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -107,7 +107,11 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
/* 访问元素 */
|
||||
var num = list[1];
|
||||
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -203,7 +207,21 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
/* 清空列表 */
|
||||
list = [];
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push(1);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(5);
|
||||
list.push(4);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.splice(3, 0, 6);
|
||||
|
||||
/* 删除元素 */
|
||||
list.splice(3, 1);
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -295,7 +313,17 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
/* 通过索引遍历列表 */
|
||||
var count = 0;
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
count++;
|
||||
}
|
||||
|
||||
/* 直接遍历列表元素 */
|
||||
count = 0;
|
||||
for (const n of list) {
|
||||
count++;
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -362,7 +390,9 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
|
||||
/* 拼接两个列表 */
|
||||
var list1 = [ 6, 8, 7, 10, 9 ];
|
||||
list.push(...list1)
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -417,7 +447,8 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="list.js"
|
||||
|
||||
/* 排序列表 */
|
||||
list.sort((a, b) => a - b); // 排序后,列表元素从小到大排列
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@@ -713,7 +744,105 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="my_list.js"
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
nums; // 数组(存储列表元素)
|
||||
_capacity = 10; // 列表容量
|
||||
_size = 0; // 列表长度(即当前元素数量)
|
||||
extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
constructor() {
|
||||
this.nums = new Array(this._capacity);
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
get size() {
|
||||
return this._size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
get 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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
Reference in New Issue
Block a user