Skip to main content

双端链表

实现

// 双端链表的节点继承 Node
export class DoublyNode<T> extends Node<T> {
constructor(
public element: T,
public next?: DoublyNode<T> | null,
public prev?: DoublyNode<T> | null,
) {
super(element, next)
this.element = element
this.next = next
this.prev = prev
}
}

// 双端链表继承单链表类
export class DoublyLinkedList<T> extends LinkedList<T> {
protected tail: DoublyNode<T> | null

constructor() {
super()
this.tail = null
}

public push(element: T) {
const node = new DoublyNode(element)
if (this.isEmpty()) {
this.head = node
this.tail = node
} else {
if (this.tail) {
this.tail.next = node
node.prev = this.tail
this.tail = node
}
}
this.count++
}
}