From 048bfd3e4d559ab3d95f24dbce91727fea6ff49b Mon Sep 17 00:00:00 2001 From: blobt Date: Wed, 31 Mar 2021 14:14:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90list=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/List.ts | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100755 src/List.ts diff --git a/src/List.ts b/src/List.ts new file mode 100755 index 00000000..58875cb9 --- /dev/null +++ b/src/List.ts @@ -0,0 +1,167 @@ +import { ListNode } from "./ListNode"; + +export class List{ + private _headNode: ListNode; + private _length: number; + public constructor() { + this._headNode = new ListNode(); + this._headNode.next = this._headNode; + this._headNode.prev = this._headNode; + this._length = 0; //头节点不计算 + } + + /** + * 判断链表是否为空 + * @returns + */ + public empty(): boolean { + return this._headNode.next === this._headNode; + } + + /** + * 获取链表长度 + */ + public get length(): number { + return this._length; + } + + /** + * 模拟c++的迭代器 + * @returns 获取第一个节点 + */ + public begin(): ListNode { + if (this._headNode.next === null) { + throw new Error("头节点不能为null"); + } + return this._headNode.next; + } + + /** + * + * @returns 模拟c++的迭代器返回头节点 + */ + public end(): ListNode { + return this._headNode; + } + + /** + * 判断是否包含某个值 + * @param data + * @returns + */ + public contains(data: T): boolean { + for (let link: ListNode | null = this._headNode.next; link !== null && link != this._headNode; link = link.next) { + if (link !== null) { + if (link.data !== undefined) { + if (data === link.data) { + return true; + } + } + } + } + return false; + } + + /** + * 遍历操作 + * @param cb + */ + public forNext(cb: (data: T) => void): void { + for (let link: ListNode | null = this._headNode.next; link !== null && link !== this._headNode; link = link.next) { + if (link.data !== undefined) { + cb(link.data); + } + } + } + + /** + * 遍历操作 + * @param cb + */ + public forPrev(cb: (data: T) => void): void { + for (let link: ListNode | null = this._headNode.next; link !== null && link !== this._headNode; link = link.prev) { + if (link.data !== undefined) { + cb(link.data); + } + } + } + + /** + * 前插 + * @param node 你要在那个节点前插入 + * @param data 要插入的数据 + * @returns + */ + public insertBefore(node: ListNode, data: T): ListNode { + let ret: ListNode = new ListNode(data); + ret.next = node; + ret.prev = node.prev; + + if (node.prev !== null) { + node.prev.next = ret; + } + + node.prev = ret; + this._length++; + return ret; + } + + /** + * 删除节点 + * @param node + */ + public remove(node: ListNode): void { + let next: ListNode | null = node.next; + let prev: ListNode | null = node.prev; + + if (prev !== null) { + prev.next = next; + } + + if (next !== null) { + next.prev = prev; + } + this._length--; + } + + /** + * 尾插 + * @param data + */ + public push(data: T): void { + this.insertBefore(this.end(), data); + } + + /** + * 弹出尾部数据 + * @returns + */ + public pop(): T | undefined { + let ret: T | undefined = undefined; + let prev: ListNode | null = this.end().prev; + if (prev !== null) { + ret = prev.data; + this.remove(prev); + } + return ret; + } + + /** + * 前插 + * @param data + */ + public push_fornt(data: T): void { + this.insertBefore(this.begin(), data); + } + + /** + * 弹出头部数据 + * @returns + */ + public pop_front(): T | undefined { + let ret: T | undefined = undefined; + ret = this.begin().data; + this.remove(this.begin()); + return ret; + } +} \ No newline at end of file