blobt
4 years ago
1 changed files with 167 additions and 0 deletions
-
167src/List.ts
@ -0,0 +1,167 @@ |
|||
import { ListNode } from "./ListNode"; |
|||
|
|||
export class List<T>{ |
|||
private _headNode: ListNode<T>; |
|||
private _length: number; |
|||
public constructor() { |
|||
this._headNode = new ListNode<T>(); |
|||
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<T> { |
|||
if (this._headNode.next === null) { |
|||
throw new Error("头节点不能为null"); |
|||
} |
|||
return this._headNode.next; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @returns 模拟c++的迭代器返回头节点 |
|||
*/ |
|||
public end(): ListNode<T> { |
|||
return this._headNode; |
|||
} |
|||
|
|||
/** |
|||
* 判断是否包含某个值 |
|||
* @param data |
|||
* @returns |
|||
*/ |
|||
public contains(data: T): boolean { |
|||
for (let link: ListNode<T> | 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<T> | 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<T> | 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<T>, data: T): ListNode<T> { |
|||
let ret: ListNode<T> = new ListNode<T>(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<T>): void { |
|||
let next: ListNode<T> | null = node.next; |
|||
let prev: ListNode<T> | 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<T> | 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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue