Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b052392

Browse files
committed
更新单向链表类
1 parent fb1ba06 commit b052392

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

‎src/LinkedList/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LinkedList from './linkedlist';
1+
import {LinkedList }from './linkedList';
22

33
// ----- 单向链表结构测试 -----//
44
console.log('// ----- 单向链表结构测试 START -----//');
@@ -14,30 +14,30 @@ console.log(linkedList);
1414
console.log(linkedList.toString()); //--> AA BB CC
1515

1616
// 测试 insert 方法
17-
linkedList.insert(0, "123");
18-
linkedList.insert(2, "456");
17+
linkedList.insert(0, '123');
18+
linkedList.insert(2, '456');
1919
console.log(linkedList.toString()); //--> 123 AA 456 BB CC
2020

2121
// 测试 getData 方法
2222
console.log(linkedList.getData(0)); //--> 123
2323
console.log(linkedList.getData(1)); //--> AA
2424

2525
// 测试 indexOf 方法
26-
console.log(linkedList.indexOf("AA")); //--> 1
27-
console.log(linkedList.indexOf("ABC")); //--> -1
26+
console.log(linkedList.indexOf('AA')); //--> 1
27+
console.log(linkedList.indexOf('ABC')); //--> -1
2828

2929
// 测试 update 方法
30-
linkedList.update(0, "12345");
30+
linkedList.update(0, '12345');
3131
console.log(linkedList.toString()); //--> 12345 AA 456 BB CC
32-
linkedList.update(1, "54321");
32+
linkedList.update(1, '54321');
3333
console.log(linkedList.toString()); //--> 12345 54321 456 BB CC
3434

3535
// 测试 removeAt 方法
3636
linkedList.removeAt(3);
3737
console.log(linkedList.toString()); //--> 12345 54321 456 CC
3838

3939
// 测试 remove 方法
40-
linkedList.remove("CC");
40+
linkedList.remove('CC');
4141
console.log(linkedList.toString()); //--> 12345 54321 456
4242

4343
// 测试 isEmpty 方法

‎src/LinkedList/linkedlist.js renamed to ‎src/LinkedList/linkedList.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1+
// 封装节点类
2+
export class Node {
3+
constructor(data) {
4+
this.data = data;
5+
this.next = null;
6+
}
7+
}
8+
19
// 单向链表结构的封装
2-
export defaultclass LinkedList {
10+
export class LinkedList {
311

412
constructor() {
513
// 初始链表长度为 0
614
this.length = 0;
715

816
// 初始 head 为 null,head 指向链表的第一个节点
917
this.head = null;
10-
11-
// 内部类(链表里的节点 Node)
12-
this.Node = class {
13-
14-
constructor(data) {
15-
this.data = data;
16-
this.next = null;
17-
}
18-
};
19-
2018
}
2119

2220
// ------------ 链表的常见操作 ------------ //
2321

2422
// append() 往链表尾部追加数据
2523
append(data) {
2624
// 1、创建新节点
27-
const newNode = new this.Node(data);
25+
const newNode = new Node(data);
2826

2927
// 2、追加新节点
3028
if (this.length === 0) {
@@ -58,7 +56,7 @@ export default class LinkedList {
5856
if (position < 0 || position > this.length) return false;
5957

6058
// 2、创建新节点
61-
const newNode = new this.Node(data);
59+
const newNode = new Node(data);
6260

6361
// 3、插入节点
6462
if (position === 0) {
@@ -206,3 +204,5 @@ export default class LinkedList {
206204
return result;
207205
}
208206
}
207+
208+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /