-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleLinkedList.js
More file actions
186 lines (169 loc) · 5.16 KB
/
DoubleLinkedList.js
File metadata and controls
186 lines (169 loc) · 5.16 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* 定义节点
*/
function Node(element) {
this.element = element;
this.next = null;
this.prev = null;
}
/*
* 定义双向链表
*/
module.exports = function DoubleLinkedList() {
// 定义属性
this.head = null;
this.tail = null;
this.length = 0;
// 尾部添加元素
DoubleLinkedList.prototype.append = function(element) {
// 创建节点
var newNode = new Node(element);
// 插入元素
if(this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
// 最后更新长度
this.length++;
}
// 在任意位置上插入数据
DoubleLinkedList.prototype.insert = function(position, element) {
// 判断越界的问题
if (position < 0 || position > this.length) return false
// 创建节点
var newNode = new Node(element);
if(position === 0) {
// 判断头结点是否为空
if(this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}
} else if(position === this.length ){
// 当前插入到链表最后
// 思考: 这种情况是否需要判断链表为空的情况呢? 答案是不需要, 为什么?
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
} else {
// 在中间插入数据
var index = 0;
var current = this.head;
var previous = null;
while(index++ < position) {
previous = current;
current = current.next;
}
// 插入节点
newNode.next = current;
current.prev = newNode;
previous.next = newNode;
newNode.prev = previous;
}
// 更新长度
this.length++;
return true;
}
// 根据位置删除对应的元素
DoubleLinkedList.prototype.removeAt = function (position) {
// 判断越界的问题
if (position < 0 || position >= this.length) return null;
// 判断移除的位置
var current = this.head;
if (position === 0) {
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.next;
this.head.prev = null;
}
} else if (position === this.length -1) {
current = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
} else {
var index = 0;
var previous = null;
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
// 更新长度
this.length--;
return current.element;
}
// 根据元素获取在链表中的位置
DoubleLinkedList.prototype.indexOf = function (element) {
// 定义变量保存信息
var current = this.head;
var index = 0;
// 查找正确的信息
while (current) {
if (current.element === element) {
return index;
}
index++;
current = current.next;
}
// 来到这个位置, 说明没有找到, 则返回-1
return -1;
}
// 根据元素删除
DoubleLinkedList.prototype.remove = function (element) {
var index = this.indexOf(element);
if(index !== -1)
return this.removeAt(index);
}
// 判断是否为空
DoubleLinkedList.prototype.isEmpty = function () {
return this.length === 0;
}
// 获取链表长度
DoubleLinkedList.prototype.size = function () {
return this.length;
}
// 获取第一个元素
DoubleLinkedList.prototype.getHead = function () {
return this.head.element;
}
// 获取最后一个元素
DoubleLinkedList.prototype.getTail = function () {
return this.tail.element;
}
// 遍历方法的实现
// 正向遍历的方法
DoubleLinkedList.prototype.forwardString = function () {
var current = this.head;
var forwardStr = "";
while (current) {
forwardStr += ", " + current.element;
current = current.next;
}
return forwardStr.slice(1);
}
// 反向遍历的方法
DoubleLinkedList.prototype.reverseString = function () {
var current = this.tail;
var reverseStr = "";
while (current) {
reverseStr += ", " + current.element;
current = current.prev;
}
return reverseStr.slice(1);
}
// 实现toString方法
DoubleLinkedList.prototype.toString = function () {
return this.forwardString();
}
}