<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
class DoubleLinkedList {
head = null;
tail = null;
length = 0;
static Node = class Node {
constructor(key) {
this.key = key;
this.next = null;
this.prev = null;
}
};
append(key) {
let newNode = new DoubleLinkedList.Node(key);
if (this.length === 0) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length += 1;
return newNode.key;
}
insert(position, key) {
if (position < 0 || position > this.length) {
return -1;
}
let newNode = new DoubleLinkedList.Node(key);
if (this.length === 0) {
this.head = newNode;
this.tail = newNode;
} else {
if (position === 0) {
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
} else if (position === this.length - 1) {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
} else {
let len = Math.floor(this.length / 2);
if (len > position) {
let current = this.head;
let index = 0;
while (index++ < position) {
current = current.next;
}
newNode.prev = current.prev;
newNode.next = current;
current.prev.next = newNode;
current.prev = newNode;
} else {
let current = this.tail;
let index = this.length - 1;
while (index-- > position) {
current = current.prev;
}
newNode.prev = current.prev;
newNode.next = current;
current.prev.next = newNode;
current.prev = newNode;
}
}
}
this.length += 1;
return newNode.key;
}
forwardString() {
let current = this.head;
let str = "";
while (current) {
str += current.key + "=>";
current = current.next;
}
return str;
}
backwardString() {
let current = this.tail;
let str = "";
while (current) {
str += current.key + "=>";
current = current.prev;
}
return str;
}
indexOf(key) {
let current = this.head;
let index = 0;
while (current) {
if (current.key === key) {
return index;
}
index += 1;
current = current.next;
}
return -1;
}
get(position) {
if (position < 0 || position > this.length - 1) {
return -1;
}
if (position === 0) {
return this.head.key;
}
if (position === this.length - 1) {
return this.tail.key;
}
let index = 0;
let current = null;
let len = Math.floor(this.length / 2);
if (len > position) {
current = this.head;
while (index++ < position) {
current = current.next;
}
} else {
current = this.tail;
index = this.length - 1;
while (index-- > position) {
current = current.prev;
}
}
return current.key;
}
has(key) {
let current = this.head;
while (current) {
if (current.key === key) {
return true;
}
current = current.next;
}
return false;
}
remove(key) {
let position = this.indexOf(key)
return this.removeAt(position)
}
removeAt(position) {
if (position < 0 || position > this.length - 1) {
return -1;
}
let current = this.head;
let index = 0;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
if (position === 0) {
this.head.next.prev = null;
this.head = this.head.next;
} else if (position === this.length - 1) {
current = this.tail
this.tail.prev.next = null;
this.tail = this.tail.prev;
} else {
while (index++ < position) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev
}
}
this.length -= 1
return current.key;
}
getHead() {
return this.head;
}
getTail() {
return this.tail;
}
size() {
return this.length;
}
isEmpty() {
return this.length === 0 ? true : false;
}
toString() {
return this.forwardString();
}
}
let d = new DoubleLinkedList();
d.append("AAA");
d.append("BBB");
d.append("CCC");
d.insert(0, "AAA");
d.insert(0, "BBB");
d.insert(1, "CCC");
d.insert(3, "DDD");
// console.log("get", d.get(0));
// console.log("get", d.get(6));
// console.log("get", d.get(3));
// console.log(d.indexOf("AAA"));
// console.log("length", d.size());
// console.log("isempty", d.isEmpty());
// console.log("has", d.has("AAA"));
// console.log("has", d.has("0"));
// console.log("has", d.has("DDD"));
// console.log("has", d.has("BBB"));
console.log("removeAt", d.removeAt(0));
console.log("removeAt", d.removeAt(1));
console.log("removeAt", d.removeAt(4));
console.log("removeAt", d.removeAt(3));
console.log("removeAt", d.removeAt(2));
console.log("length", d.size());
console.log("remove ", d.remove('DDD'));
console.log(d.toString());
console.log(d);
</script>
</body>
</html>
【算法】双链表
最新推荐文章于 2026-01-05 22:36:07 发布
该文章展示了一个JavaScript类,用于创建和操作双链表。类包含方法如添加元素、在指定位置插入元素、删除元素、向前和向后遍历链表、获取元素、检查元素存在性以及获取链表的长度等。
3023

被折叠的 条评论
为什么被折叠?



