asynciterator
Version:
An asynchronous iterator library for advanced object pipelines.
37 lines (36 loc) • 1.05 kB
JavaScript
/**
* A list with O(1) push and shift operations.
*/
export class LinkedList {
constructor() {
this._length = 0;
this._head = null;
this._tail = null;
}
get length() { return this._length; }
get first() { var _a; return (_a = this._head) === null || _a === void 0 ? void 0 : _a.value; }
get last() { var _a; return (_a = this._tail) === null || _a === void 0 ? void 0 : _a.value; }
get empty() { return this._head === null; }
push(value) {
const node = { value, next: null };
if (this._tail === null)
this._head = this._tail = node;
else
this._tail.next = this._tail = node;
this._length++;
}
shift() {
if (this._head === null)
return undefined;
const { value, next } = this._head;
this._head = next;
if (next === null)
this._tail = null;
this._length--;
return value;
}
clear() {
this._length = 0;
this._head = this._tail = null;
}
}