From 88bbfdc4707e6cafb5b8f9240fc6613356a45029 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Mon, 6 Aug 2018 16:12:31 +0300 Subject: [PATCH] Add fromArray() methods to LinkedList and DoublyLinkedList. --- .../doubly-linked-list/DoublyLinkedList.js | 9 +++++---- .../__test__/DoublyLinkedList.test.js | 18 ++++-------------- src/data-structures/linked-list/LinkedList.js | 10 ++++++++++ .../linked-list/__test__/LinkedList.test.js | 7 +++++++ 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/data-structures/doubly-linked-list/DoublyLinkedList.js b/src/data-structures/doubly-linked-list/DoublyLinkedList.js index 6b7f908e..4afb404b 100644 --- a/src/data-structures/doubly-linked-list/DoublyLinkedList.js +++ b/src/data-structures/doubly-linked-list/DoublyLinkedList.js @@ -214,11 +214,12 @@ export default class DoublyLinkedList { } /** - * @param {DoublyLinkedListNode[]} array - Array of nodes - * @return {DoublyLinkedListNode[]} + * @param {*[]} values - Array of values that need to be converted to linked list. + * @return {DoublyLinkedList} */ - fromArray(arr = []) { - arr.forEach(node => this.append(node.value)); + fromArray(values) { + values.forEach(value => this.append(value)); + return this; } diff --git a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js index d036e3f5..5e4e81ea 100644 --- a/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js +++ b/src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js @@ -36,21 +36,11 @@ describe('DoublyLinkedList', () => { expect(linkedList.toString()).toBe('3,2,1'); }); - it('should append new nodes from array', () => { - const linkedList1 = new DoublyLinkedList(); - linkedList1.append(1); - linkedList1.append(1); - linkedList1.append(2); - linkedList1.append(3); - linkedList1.append(3); - linkedList1.append(3); - linkedList1.append(4); - linkedList1.append(5); - const array = linkedList1.toArray(); + it('should create linked list from array', () => { + const linkedList = new DoublyLinkedList(); + linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]); - const linkedList2 = new DoublyLinkedList(); - linkedList2.fromArray(array); - expect(linkedList2.toString()).toBe('1,1,2,3,3,3,4,5'); + expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5'); }); it('should delete node by value from linked list', () => { diff --git a/src/data-structures/linked-list/LinkedList.js b/src/data-structures/linked-list/LinkedList.js index 587177f9..dfb17aaf 100644 --- a/src/data-structures/linked-list/LinkedList.js +++ b/src/data-structures/linked-list/LinkedList.js @@ -175,6 +175,16 @@ export default class LinkedList { return deletedHead; } + /** + * @param {*[]} values - Array of values that need to be converted to linked list. + * @return {LinkedList} + */ + fromArray(values) { + values.forEach(value => this.append(value)); + + return this; + } + /** * @return {LinkedListNode[]} */ diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index 384e44fa..376a2e01 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -184,6 +184,13 @@ describe('LinkedList', () => { expect(linkedList.find({ callback: value => value.key === 'test5' })).toBeNull(); }); + it('should create linked list from array', () => { + const linkedList = new LinkedList(); + linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]); + + expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5'); + }); + it('should find node by means of custom compare function', () => { const comparatorFunction = (a, b) => { if (a.customValue === b.customValue) {