//实现一个链表
class LinkNode<T> {
constructor(public value: T, public next: LinkNode<T> | null = null
, public prev: LinkNode<T> | null = null) {
}
}
class LinkList<T> {
public head: LinkNode<T> | null;
public end: LinkNode<T> | null;
public add(value: LinkNode<T>): void {
if (this.head) {
this.end.next = value;
value.prev = this.end;
this.end = value;
} else {
this.head = value;
this.end = value;
}
}
//实现取出头部的元素的方法
public pop(): T | null {
if (!this.head) return null;
let value = this.head.value;
this.head = this.head.next;
this.head.prev = null;
return value
}
//实现取出尾部元素的方法
public shift(): T | null {
if (!this.end) return null;
let value = this.end.value;
this.end = this.end.prev;
this.end.next = null;
return value;
}
// 实现头部插入的方法
public unshift(value: LinkNode<T>) {
if (this.head) {
this.head.prev = value;
value.next = this.head;
this.head = value;
} else {
this.head = value;
}
}
}
/**
* 再用数组实现一个队列,循环数组
*/
class Queue {
public arr: number[];
private headIndex: number = 0;
private endIndex: number = 0;
private size: number = 0;
constructor(public len: number) {
this.arr = new Array(len).fill(null);
}
// 实现添加元素的方法
public push(value: number): void {
if (this.size === this.len) {
console.log('队列已满');
return;
}
this.arr[this.endIndex] = value;
this.endIndex = this.getNextIndex(this.endIndex);
}
//实现一个取出顶部元素的方法
public pop(): number | null {
if (this.size === 0) return null;
let value = this.arr[this.headIndex];
this.arr[this.headIndex] = null;
this.headIndex = this.getNextIndex(this.headIndex);
return value;
}
private getNextIndex(index: number): number {
return (index + 1) % this.arr.length;
}
}
//再实现一个可以取出最小值都是O(1)的栈和队列用链表实现
class MinStack<T> {
public head: LinkNode<T> | null;
public end: LinkNode<T> | null;
public head1: LinkNode<T> | null;
public end1: LinkNode<T> | null;
public add(value: LinkNode<T>): void {
if (this.head) {
this.end.next = value;
value.prev = this.end;
this.end = value;
//深拷贝value
let dad = value.value > this.end1.value ? this.end1.value : value.value;
let value1 = new LinkNode(dad);
this.end1.next = value1;
value1.prev = this.end1
} else {
this.head = value;
this.end = value;
this.head1 = value;
this.end1 = value;
}
}
public getMin(): T | null {
if (!this.head1) return null;
return this.end1.value;
}
//实现取出头部的元素的方法
public pop(): T | null {
if (!this.head) return null;
let value = this.head.value;
this.head = this.head.next;
this.head.prev = null;
this.head1 = this.head1.next;
this.head1.prev = null;
return value
}
//实现取出尾部元素的方法
public shift(): T | null {
if (!this.end) return null;
let value = this.end.value;
this.end = this.end.prev;
this.end.next = null;
this.end1 = this.end1.prev;
this.end1.next = null;
return value;
}
// 实现头部插入的方法
public unshift(value: LinkNode<T>) {
if (this.head) {
this.head.prev = value;
value.next = this.head;
this.head = value;
this.head1 = value;
} else {
this.head = value;
this.head1 = value;
this.end = value;
this.end1 = value;
}
}
}
typescript 实现双向链表的队列和栈功能,通过数组实现队列功能,通过双链表实现O(1)复杂度的获取链表中最小值功能
于 2025-04-11 21:47:40 首次发布