* New function 'fromArray'

Function get array of Doubly Linked List Nodes, go through and append to currently list.

* New Test for new function 'fromArray'

* Minor changes

Minor changes about coding style.
This commit is contained in:
Simon 2018-08-06 15:02:46 +02:00 committed by Oleksii Trekhleb
parent 7a4b829abe
commit 0ea24230d4
2 changed files with 26 additions and 0 deletions

View File

@ -213,6 +213,15 @@ export default class DoublyLinkedList {
return nodes; return nodes;
} }
/**
* @param {DoublyLinkedListNode[]} array - Array of nodes
* @return {DoublyLinkedListNode[]}
*/
fromArray(arr = []) {
arr.forEach(node => this.append(node.value));
return this;
}
/** /**
* @param {function} [callback] * @param {function} [callback]
* @return {string} * @return {string}

View File

@ -36,6 +36,23 @@ describe('DoublyLinkedList', () => {
expect(linkedList.toString()).toBe('3,2,1'); 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();
const linkedList2 = new DoublyLinkedList();
linkedList2.fromArray(array);
expect(linkedList2.toString()).toBe('1,1,2,3,3,3,4,5');
});
it('should delete node by value from linked list', () => { it('should delete node by value from linked list', () => {
const linkedList = new DoublyLinkedList(); const linkedList = new DoublyLinkedList();