UNPKG

stack-base-iterator

Version:

Base iterator for values retrieved using a stack of async functions returning values

69 lines (68 loc) 1.97 kB
let Node = class Node { constructor(value){ this.value = value; this.prev = null; this.next = null; } }; let LinkedList = class LinkedList { last() { return this.tail ? this.tail.value : undefined; } push(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { newNode.prev = this.tail; this.tail.next = newNode; this.tail = newNode; } this.length++; return this; } pop() { if (!this.head) return undefined; const poppedNode = this.tail; if (this.length === 1) { this.head = null; this.tail = null; } else { this.tail = poppedNode.prev; this.tail.next = null; poppedNode.prev = null; } this.length--; return poppedNode.value; } remove(value) { if (!this.head) return undefined; let currentNode = this.head; while(currentNode){ if (currentNode.value === value) { if (currentNode === this.head) { this.head = currentNode.next; if (this.head) this.head.prev = null; else this.tail = null; } else if (currentNode === this.tail) { this.tail = currentNode.prev; this.tail.next = null; } else { currentNode.prev.next = currentNode.next; currentNode.next.prev = currentNode.prev; } this.length--; return currentNode.value; } currentNode = currentNode.next; } return undefined; } constructor(){ this.head = null; this.tail = null; this.length = 0; } }; export { LinkedList as default };