-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBothwayLinkedList.ts
91 lines (90 loc) · 2.79 KB
/
BothwayLinkedList.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
// 节点类
class Node {
data: any;
prior!: Node;
next!: Node;
constructor(value: any) {
this.data = value;
}
}
// 双向链表
class BothwayLinkedList {
// 头结点
head: Node;
constructor() {
let node = new Node('head');
node.prior = node;
node.next = node;
this.head = node;
}
// 尾部插入
append(value: any) {
let currentNode = this.head;
const newNode = new Node(value);
while (currentNode.next !== this.head) {
currentNode = currentNode.next;
}
newNode.prior = currentNode;
newNode.next = this.head;
currentNode.next = newNode;
}
print() {
let currentNode = this.head;
while (currentNode.next !== this.head) {
currentNode = currentNode.next;
console.log(currentNode.data);
}
}
// 指定节点后插入
insertAfterNode(value: any, newValue: any) {
let currentNode = this.head;
const newNode = new Node(newValue);
while (currentNode.next !== this.head) {
currentNode = currentNode.next;
if (currentNode.data === value) {
newNode.prior = currentNode;
newNode.next = currentNode.next;
currentNode.next = newNode;
return;
}
}
throw new Error('The specified node does not exist!');
}
// 删除指定节点
removeByData(value: any) {
let currentNode = this.head;
while (currentNode.next !== this.head) {
currentNode = currentNode.next;
console.log(currentNode);
if (currentNode.data === value) {
let priorNode = currentNode.prior;
priorNode.next = currentNode.next;
return;
}
}
throw new Error('The specified node does not exist!');
}
// 获取元素位置
indexOf(value: any) {
let index = 0;
let currentNode = this.head;
while (currentNode.next !== this.head) {
currentNode = currentNode.next;
if (currentNode.data === value) {
return index
}
index++;
}
return -1;
}
}
const list = new BothwayLinkedList();
list.append(1);
list.append(3);
list.append(2);
// list.insertAfterNode(3, 4);
list.removeByData(2);
list.print();
console.log('元素位置 :', list.indexOf(2));
}