Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the remain JavaScript code and docs and Update the TypeScript style for Chapter of Array and Linkedlist #95

Merged
merged 7 commits into from
Dec 12, 2022
88 changes: 43 additions & 45 deletions codes/javascript/chapter_array_and_linkedlist/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
*/

/* 随机访问元素 */
function randomAccess(nums){
function randomAccess(nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length)
const random_index = Math.floor(Math.random() * nums.length);
// 获取并返回随机元素
random_num = nums[random_index]
return random_num
const random_num = nums[random_index];
return random_num;
}

/* 扩展数组长度 */
// 请注意,Pythonlist 是动态数组,可以直接扩展
// 为了方便学习,本函数将 list 看作是长度不可变的数组
function extend(nums, enlarge){
// 初始化一个扩展长度后的数组
let res = new Array(nums.length + enlarge).fill(0)
// 将原数组中的所有元素复制到新数组
for(let i=0; i<nums.length;i++){
res[i] = nums[i]
}
// 请注意,JavaScriptArray 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
function extend(nums, enlarge) {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
// 将原数组中的所有元素复制到新数组
for (let i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res
return res;
}

/* 在数组的索引 index 处插入元素 num */
function insert(nums, num, index){
function insert(nums, num, index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (let i = nums.length - 1; i >= index; i--) {
nums[i] = nums[i - 1];
Expand All @@ -38,62 +38,60 @@ 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]
}
nums[i] = nums[i + 1];
}
}

/* 遍历数组 */
function traverse(nums){
let count = 0
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
return -1;
}


/* Driver Codes*/
/* 初始化数组 */
var arr = new Array(5).fill(0)
console.log("数组 arr =", arr)
var nums = [1, 3, 2, 5, 4]
console.log("数组 nums =", nums)
let arr = new Array(5).fill(0);
console.log('数组 arr =', arr);
let nums = [1, 3, 2, 5, 4];
console.log('数组 nums =', nums);

/* 随机访问 */
random_num = randomAccess(nums)
console.log("在 nums 中获取随机元素", random_num)
const random_num = randomAccess(nums);
console.log('在 nums 中获取随机元素', random_num);

/* 长度扩展 */
nums = extend(nums, 3)
console.log("将数组长度扩展至 8 ,得到 nums =", nums)
nums = extend(nums, 3);
console.log('将数组长度扩展至 8 ,得到 nums =', nums);

/* 插入元素 */
insert(nums, 6, 3)
console.log("在索引 3 处插入数字 6 ,得到 nums =", nums)
insert(nums, 6, 3);
console.log('在索引 3 处插入数字 6 ,得到 nums =', nums);

/* 删除元素 */
remove(nums, 2)
console.log("删除索引 2 处的元素,得到 nums =", nums)
remove(nums, 2);
console.log('删除索引 2 处的元素,得到 nums =', nums);

/* 遍历数组 */
traverse(nums)
traverse(nums);

/* 查找元素 */
var index = find(nums, 3)
console.log("在 nums 中查找元素 3 ,得到索引 =", index)
var index = find(nums, 3);
console.log('在 nums 中查找元素 3 ,得到索引 =', index);
84 changes: 84 additions & 0 deletions codes/javascript/chapter_array_and_linkedlist/linked_list.js
Original file line number Diff line number Diff line change
@@ -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}`);
58 changes: 58 additions & 0 deletions codes/javascript/chapter_array_and_linkedlist/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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++;
}

/* 拼接两个列表 */
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}`);
Loading