UNPKG

another-linked-list

Version:
202 lines (164 loc) 3.85 kB
/** * @description implementation of Linked List * @module LinkedList */ import LinkedListNode from './linked-list-node'; export default class LinkedList { /** * @description class constructor */ constructor() { this.head = null; this.tail = null; } /** * @public method * @description prepend value to linked list * @param {*} value * @return {LinkedList} */ prepend(value) { const newNode = new LinkedListNode(value, this.head); this.head = newNode; if (!this.tail) { this.tail = newNode; } return this; } /** * @public method * @description append value to linked list * @param {*} value * @return {LinkedList} */ append(value) { const newNode = new LinkedListNode(value); if (!this.head) { this.head = newNode; this.tail = newNode; return this; } const currentTail = this.tail; currentTail.next = newNode; this.tail = newNode; return this; } /** * @public method * @description delete value from linked list node * @param {*} value * @return {LinkedListNode} */ delete(value) { if (!this.head) { return null; } let deletedNode = null; while (this.head && this.head.value === value) { deletedNode = this.head; this.head = this.head.next; } let currentNode = this.head; if (currentNode !== null) { while (currentNode.next) { if (currentNode.next.value === value) { deletedNode = currentNode.next; currentNode.next = currentNode.next.next; } else { currentNode = currentNode.next; } } } if (this.tail.value === value) { this.tail = currentNode; } return deletedNode; } /** * @public method * @description delete tail value from linked list node * @return {LinkedListNode} */ deleteTail() { const deletedTail = this.tail; if (this.head === this.tail) { this.head = null; this.tail = null; return deletedTail; } let currentNode = this.head; while (currentNode.next) { if (!currentNode.next.next) { currentNode.next = null; } else { currentNode = currentNode.next; } } this.tail = currentNode; return deletedTail; } /** * @public method * @description delete head value from linked list * @return {LinkedListNode} */ deleteHead() { if (!this.head) { return null; } const deletedHead = this.head; if (this.head.next) { this.head = this.head.next; } else { this.head = null; this.tail = null; } return deletedHead; } /** * @public method * @description find value in linked list * @param {Object} findParams * @param {function} [findParams.callback] * @return {LinkedListNode} */ find({ value = undefined, callback = undefined }) { if (!this.head) { return null; } let currentNode = this.head; while (currentNode) { if (callback && callback(currentNode.value)) { return currentNode; } if (value !== undefined && currentNode.value === value) { return currentNode; } currentNode = currentNode.next; } return null; } /** * @public method * @description convert linked list to array * @return {LinkedListNode[]} */ toArray() { const nodes = []; let currentNode = this.head; while (currentNode) { nodes.push(currentNode); currentNode = currentNode.next; } return nodes; } /** * @public method * @description convert linked list to string * @param {function} [callback] * @return {string} */ toString(callback) { return this.toArray().map(node => node.toString(callback)).toString(); } }