这里是一个使用TypeScript实现的双向链表:
// 定义双向链表节点
class ListNode<T> {
public val: T;
public prev: ListNode<T> | null;
public next: ListNode<T> | null;
constructor(val: T, prev?: ListNode<T> | null, next?: ListNode<T> | null) {
this.val = val;
this.prev = prev || null;
this.next = next || null;
}
}
class DoublyLinkedList<T> {
public head: ListNode<T> | null;
public tail: ListNode<T> | null;
public size: number;
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
// 在链表末尾添加节点
public push(val: T): number {
const node = new ListNode(val);
if (!this.tail) {
this.head = node;
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
this.size++;
return this.size;
}
// 从链表末尾移除节点
public pop(): T | undefined {
if (!this.tail) return undefined;
const node = this.tail;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = this.tail.prev;
this.tail!.next = null;
}
this.size--;
return node.val;
}
// 在链表头部添加节点
public unshift(val: T): number {
const node = new ListNode(val);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head.prev = node;
this.head = node;
}
this.size++;
return this.size;
}
// 从链表头部移除节点
public shift(): T | undefined {
if (!this.head) return undefined;
const node = this.head;
if (this.size === 1) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.next;
this.head!.prev = null;
}
this.size--;
return node.val;
}
// 获取指定位置的节点
public get(index: number): T | undefined {
if (index < 0 || index >= this.size) return undefined;
let node = this.head;
for (let i = 0; i < index; i++) {
node = node!.next;
}
return node!.val;
}
// 设置指定位置的节点
public set(index: number, val: T): boolean {
if (index < 0 || index >= this.size) return false;
let node = this.head;
for (let i = 0; i < index; i++) {
node = node!.next;
}
node!.val = val;
return true;
}
// 在指定位置插入节点
public insert(index: number, val: T): boolean {
if (index < 0 || index > this.size) return false;
if (index === 0) {
return !!this.unshift(val);
} else if (index === this.size) {
return !!this.push(val);
} else {
const prevNode = this.getListNode(index - 1);
const nextNode = prevNode.next;
const node = new ListNode(val, prevNode, nextNode);
prevNode.next = node;
nextNode!.prev = node;
this.size++;
return true;
}
}
// 移除指定位置的节点
public remove(index: number): T | undefined {
if (index < 0 || index >= this.size) return undefined;
if (index === 0) {
return this.shift();
} else if (index === this.size - 1) {
return this.pop();
} else {
const node = this.getListNode(index);
node.prev!.next = node!.next;
node.next!.prev = node.prev;
this.size--;
return node.val;
}
}
// 获取指定位置的节点(节点对象)
private getListNode(index: number): ListNode<T> | null {
if (index < 0 || index >= this.size) return null;
let node = this.head;
for (let i = 0; i < index; i++) {
node = node!.next;
}
return node;
}
}
我们可以使用以下代码测试该双向链表的各种操作:
const doublyList = new DoublyLinkedList<number>();
console.log(doublyList.push(1)); // 1
console.log(doublyList.push(2)); // 2
console.log(doublyList.push(3)); // 3
console.log(doublyList.push(4)); // 4
console.log(doublyList.pop()); // 4
console.log(doublyList.shift()); // 1
console.log(doublyList.unshift(5)); // 3
console.log(doublyList.get(1)); // 2
console.log(doublyList.set(1, 6)); // true
console.log(doublyList.insert(2, 7)); // true
console.log(doublyList.remove(1)); // 6