与之前链表结构类似,不过之前单向链表相比之下双向会多一个引用指针,分别来指向前一个跟后一个对象
我们用一个 pre 跟 next 来记录
1、首先还是创建一个 Node
class Node {
constructor(node) {
this.node = node;
this.prev = null;
this.next = null;
}
}
其次创建双向链表方法
class DoublyLinked {
__head = null
__tail = null
__length = 0
}
这里我们定义 私有变量 __head、__tail、__length 分别表是 指向第一个元素,最后一个元素以及当前链表的长度大小
接下来是插入方法
append(node) {
//首先创建一个节点数据
const newNode = new Node(node);
//判断当前链表是否为空
// 空 :直接将 __head __tail 分别指向这个节点即可
// 非空:分别设置引用,先将新的节点的 prev 指向最后一个节点,然后将 __tail 指向创建的新节点
if (this.isEmpty()) {
this.__head = newNode;
this.__tail = newNode;
} else {
newNode.prev = this.__tail
this.__tail.next = newNode;
this.__tail = newNode;
}
this.__length += 1
}
insert 插入节点,相对来就比较复杂了,考虑到的会比较多
insert(pos, node) {
// 越界判断
if (pos < 0 || pos > this.size()) { return }
const newNode = new Node(node)
// 空时,直接插入
if (this.isEmpty()) {
this.__head = newNode;
this.__tail = newNode;
} else {
// 下标为 0 时 此时的 __head 就是 newNode 的下一个节点
// - 将第一个 __head 的 prev 指向 newNode
// - 新节点的 next 指向 __head
// - __head 指向 newNode
if (pos === 0) {
this.__head.prev = newNode;
newNode.next = this.__head
this.__head = newNode
// 下标等于长度时,就追加,跟 append 类似 不过 用 __tail 最后一个节点
} else if (pos === this.size()) {
newNode.prev = this.__tail
this.__tail.next = newNode
this.__tail = newNode
} else {
// 找到对应的节点,分别移动 next 及 prev 的指向引用
let currentNode = this.__head
let index = 0
while (index++ < pos) {
currentNode = currentNode.next
}
newNode.next = currentNode
newNode.prev = currentNode.prev
currentNode.prev.next = newNode
currentNode.prev = newNode
}
}
this.__length += 1
return true
}
get(pos) {
// 越界判断
if (pos < 0 || pos >= this.size()) { return null }
const before = this.size() / 2 > pos
let current = this.__head
let index = 0
if (before) {
while (index++ < pos) {
current = current.next
}
} else {
console.log(2);
current = this.__tail
index = this.size() - 1
while (index-- > pos) {
current = current.prev
}
}
return current.node
}
其他方法
indexOf(node) {
let currentNode = this.__head
let index = 0
while (currentNode) {
if (currentNode.node === node) {
return index
}
currentNode = currentNode.next
index += 1
}
return -1
}
update(pos, node) {
// 越界判断
if (pos < 0 || pos >= this.size()) { return false }
let currentNode = this.__head
let index = 0
while (index++ < pos) {
currentNode = currentNode.next
}
currentNode.node = node
return true
}
removeAt(pos) {
// 越界判断
if (pos < 0 || pos >= this.size()) return false
let currentNode = this.__head
// 判断是否是只有一个节点
if (this.size() === 1) {
this.__head = null
this.__tail = null
} else {
// 判断删除的下标
if (pos === 0) {
// 找到下一个节点
this.__head.next.prev = null
this.__head = this.__head.next
} else if (pos === this.size() - 1) {
current = this.__tail
this.__tail.prev.next = null
this.__tail = this.__tail.prev
} else {
let index = 0
while (index++ < pos) {
currentNode = currentNode.next
}
currentNode.prev.next = currentNode.next
currentNode.next.prev = currentNode.prev
}
}
this.__length -= 1
return current.node
}
remove(node) {
// 获取对应下标值
let index = this.indexOf(node)
return this.removeAt(index)
}
isEmpty() { return this.size() === 0 }
size() { return this.__length }
toString() {
return this.backwardString()
}
forwardString() {
let current = this.__tail
let str = ''
while (current) {
str += current.node + ' '
current = current.prev
}
return str
}
backwardString() {
let current = this.__head
let str = ''
while (current) {
str += current.node + ' '
current = current.next
}
return str
}