🌈 style: 修改代码格式
This commit is contained in:
parent
c8de724698
commit
9f333ecc87
@ -4,8 +4,8 @@ interface Node<T> {
|
||||
next?: Node<T>;
|
||||
}
|
||||
export class DoubleQueue<T> {
|
||||
protected head?: Node<T>;
|
||||
protected tail?: Node<T>;
|
||||
protected _head?: Node<T>;
|
||||
protected _tail?: Node<T>;
|
||||
protected _size = 0;
|
||||
constructor(it?: Iterable<T>) {
|
||||
if (it) {
|
||||
@ -20,51 +20,51 @@ export class DoubleQueue<T> {
|
||||
push(item: T) {
|
||||
const node: Node<T> = { value: item };
|
||||
if (this.size) {
|
||||
(this.tail!.next = node).prev = this.tail;
|
||||
this.tail = node;
|
||||
(this._tail!.next = node).prev = this._tail;
|
||||
this._tail = node;
|
||||
} else {
|
||||
this.head = this.tail = node;
|
||||
this._head = this._tail = node;
|
||||
}
|
||||
this._size++;
|
||||
}
|
||||
pop(): T | undefined {
|
||||
if (!this.size) return undefined;
|
||||
const node = this.tail!;
|
||||
const node = this._tail!;
|
||||
if (this.size === 1) {
|
||||
this.head = this.tail = undefined;
|
||||
this._head = this._tail = undefined;
|
||||
} else {
|
||||
node.prev!.next = undefined;
|
||||
}
|
||||
this._size--;
|
||||
if (this.size === 1) this.head = this.tail;
|
||||
if (this.size === 1) this._head = this._tail;
|
||||
return node.value;
|
||||
}
|
||||
unshift(item: T) {
|
||||
const node: Node<T> = { value: item };
|
||||
if (this.size) {
|
||||
(this.head!.prev = node).next = this.head;
|
||||
this.head = node;
|
||||
(this._head!.prev = node).next = this._head;
|
||||
this._head = node;
|
||||
} else {
|
||||
this.head = this.tail = node;
|
||||
this._head = this._tail = node;
|
||||
}
|
||||
this._size++;
|
||||
}
|
||||
shift(): T | undefined {
|
||||
if (!this.size) return undefined;
|
||||
const node = this.head!;
|
||||
const node = this._head!;
|
||||
if (this.size === 1) {
|
||||
this.head = this.tail = undefined;
|
||||
this._head = this._tail = undefined;
|
||||
} else {
|
||||
node.next!.prev = undefined;
|
||||
}
|
||||
this._size--;
|
||||
if (this.size === 1) this.tail = this.head;
|
||||
if (this.size === 1) this._tail = this._head;
|
||||
return node.value;
|
||||
}
|
||||
get(i: number): T | undefined {
|
||||
const reversed = i < 0;
|
||||
if (i < -this.size || i >= this.size) return undefined;
|
||||
let node = reversed ? this.tail! : this.head!;
|
||||
let node = reversed ? this._tail! : this._head!;
|
||||
if (reversed) i = -i - 1;
|
||||
for (let j = 1; j <= i; j++) {
|
||||
node = reversed ? node.prev! : node.next!;
|
||||
@ -72,7 +72,7 @@ export class DoubleQueue<T> {
|
||||
return node.value;
|
||||
}
|
||||
*iter() {
|
||||
for (let node = this.head; node; node = node.next) {
|
||||
for (let node = this._head; node; node = node.next) {
|
||||
yield node.value;
|
||||
}
|
||||
}
|
||||
@ -80,7 +80,7 @@ export class DoubleQueue<T> {
|
||||
return this.iter();
|
||||
}
|
||||
*reverseIter() {
|
||||
for (let node = this.tail; node; node = node.prev) {
|
||||
for (let node = this._tail; node; node = node.prev) {
|
||||
yield node.value;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user