Skip to content

Commit 8742e9b

Browse files
author
宋子龙
committed
LinkedList, done
1 parent 28491be commit 8742e9b

File tree

8 files changed

+73
-109
lines changed

8 files changed

+73
-109
lines changed
File renamed without changes.
File renamed without changes.

BinarySearchTree/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import BinarySearchTree from './BinarySearchTree';
2+
3+
export default BinarySearchTree;
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<title></title>
5-
</head>
6-
<body>
7-
</body>
8-
</html>
9-
<script type="text/javascript">
10-
const log = console.log
11-
121
class Node {
132
constructor(element) {
143
this.element = element;
154
this.next = null;
165
}
176
}
187

8+
let head = Symbol('head');
9+
let length = Symbol('length');
10+
1911
class LinkedList {
2012
constructor() {
21-
this.length = 0;
22-
this.head = null;
13+
this[length] = 0;
14+
this[head] = null;
2315
}
2416

2517
/** 向列表尾部添加元素*/
2618
append(element) {
2719
const node = new Node(element);
2820
let current = null;
2921

30-
if (!this.head) {
31-
this.head = node;
22+
if (!this[head]) {
23+
this[head] = node;
3224
} else {
33-
current = this.head;
25+
current = this[head];
3426

3527
// 找到最末尾的元素
3628
while (current.next) {
@@ -39,7 +31,7 @@
3931
current.next = node;
4032
}
4133

42-
this.length++;
34+
this[length]++;
4335
}
4436

4537
/** 向特定位置插入元素
@@ -49,18 +41,16 @@
4941
*/
5042
insert(position, element) {
5143
const node = new Node(element);
52-
let current = this.head,
44+
let current = this[head],
5345
previous = null;
5446

5547
const isValidPosition =
56-
(typeof position === 'number') && (position >= 0) && (position < this.length);
57-
if (!isValidPosition) {
58-
return false
59-
}
48+
(typeof position === 'number') && (position >= 0) && (position < this[length]);
49+
if (!isValidPosition) { return false }
6050

6151
if (position === 0) {
6252
node.next = current;
63-
this.head = node;
53+
this[head] = node;
6454
} else {
6555
for (let i = 0; i < position; i++) {
6656
previous = current;
@@ -70,20 +60,20 @@
7060
node.next = current;
7161
previous.next = node;
7262
}
73-
this.length++;
63+
this[length]++;
7464
return true;
7565
}
7666

7767
/** 移除指定元素
7868
* @return 移除成功时,返回被移除的元素
7969
*/
8070
remove(element) {
81-
let current = this.head,
71+
let current = this[head],
8272
previous = null;
8373

84-
if (this.head.element === element) {
85-
this.head = current.next;
86-
this.length--;
74+
if (this[head].element === element) {
75+
this[head] = current.next;
76+
this[length]--;
8777
return current.element;
8878
}
8979

@@ -93,7 +83,7 @@
9383

9484
if (current.element === element) {
9585
previous.next = current.next;
96-
this.length--;
86+
this[length]--;
9787
return current.element;
9888
}
9989
}
@@ -106,14 +96,16 @@
10696
*/
10797
removeAt(position) {
10898
const isValidPosition =
109-
(typeof position === 'number') && (position >= 0) && (position < this.length);
99+
(typeof position === 'number') && (position >= 0) && (position < this[length]);
100+
101+
if (!isValidPosition) { return null }
110102

111103
if (isValidPosition) {
112-
let current = this.head,
104+
let current = this[head],
113105
previous = null;
114106

115107
if (position === 0) {
116-
this.head = current.next;
108+
this[head] = current.next;
117109
} else {
118110
for (let i = 0; i < position; i++) {
119111
previous = current;
@@ -123,71 +115,54 @@
123115
// 将 current node 从链表 链中移除
124116
previous.next = current.next;
125117
}
126-
this.length--;
118+
this[length]--;
127119
return current.element;
128120
}
121+
}
122+
123+
/** 类似 Array.prototype.join*/
124+
join(sign = '-') {
125+
let current = this[head],
126+
result = '';
129127

130-
if (!isValidPosition) {
131-
return null
128+
while (current) {
129+
result += current.element + (current.next ? sign : '');
130+
current = current.next;
132131
}
132+
133+
return result;
133134
}
134135

135136
/** 返回元素在列表中的索引;若无改元素,返回 -1
136-
*
137+
* @return {Number} 相当于链表的下标 index
137138
*/
138139
indexOf(element) {
139-
140+
let current = this[head],
141+
index = 0;
142+
while (current) {
143+
if (current.element === element) {
144+
return index;
145+
}
146+
current = current.next;
147+
index++;
148+
}
149+
return -1;
140150
}
141151

142-
/** 检查链表为空时,返回 true
143-
*
144-
*/
152+
/** 检查链表是否为空*/
145153
isEmpty() {
146-
154+
return this[length] === 0;
147155
}
148156

149-
/** 返回链表元素个数
150-
* @return {Number}
151-
*/
157+
/** 返回链表元素个数*/
152158
size() {
153-
159+
return this[length];
154160
}
155161

162+
/** 获得头部节点*/
163+
getHead() {
164+
return this[head];
165+
}
156166
}
157167

158-
159-
const chain = new LinkedList()
160-
chain.append(1)
161-
chain.append(2)
162-
chain.append(3)
163-
164-
// chain.append({a: 123})
165-
log(chain.insert(0, 'x1'))
166-
log(chain.insert(2, 'x2'))
167-
log(chain.insert(4, 'x3'))
168-
// chain.removeAt(2)
169-
170-
log(chain)
171-
</script>
172-
173-
174-
175-
176-
177-
178-
179-
180-
181-
182-
183-
184-
185-
186-
187-
188-
189-
190-
191-
192-
193-
168+
export default LinkedList;

LinkedList/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import LinkedList from './LinkedList';
2+
3+
export default LinkedList;

graph/graph.html

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
4-
<title></title>
5+
<title></title>
56
</head>
7+
68
<body>
79
</body>
10+
811
</html>
912
<script type="text/javascript">
10-
const log = console.log
11-
12-
13-
14-
</script>
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
13+
const log = console.log
14+
</script>

sort/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import quickSort from './quickSort'
2+
3+
export {
4+
quickSort
5+
}

0 commit comments

Comments
 (0)