Skip to content

Commit 0ea2423

Browse files
seIncorptrekhleb
authored andcommitted
Patch 5 (trekhleb#127)
* 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.
1 parent 7a4b829 commit 0ea2423

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/data-structures/doubly-linked-list/DoublyLinkedList.js

+9
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ export default class DoublyLinkedList {
213213
return nodes;
214214
}
215215

216+
/**
217+
* @param {DoublyLinkedListNode[]} array - Array of nodes
218+
* @return {DoublyLinkedListNode[]}
219+
*/
220+
fromArray(arr = []) {
221+
arr.forEach(node => this.append(node.value));
222+
return this;
223+
}
224+
216225
/**
217226
* @param {function} [callback]
218227
* @return {string}

src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ describe('DoublyLinkedList', () => {
3636
expect(linkedList.toString()).toBe('3,2,1');
3737
});
3838

39+
it('should append new nodes from array', () => {
40+
const linkedList1 = new DoublyLinkedList();
41+
linkedList1.append(1);
42+
linkedList1.append(1);
43+
linkedList1.append(2);
44+
linkedList1.append(3);
45+
linkedList1.append(3);
46+
linkedList1.append(3);
47+
linkedList1.append(4);
48+
linkedList1.append(5);
49+
const array = linkedList1.toArray();
50+
51+
const linkedList2 = new DoublyLinkedList();
52+
linkedList2.fromArray(array);
53+
expect(linkedList2.toString()).toBe('1,1,2,3,3,3,4,5');
54+
});
55+
3956
it('should delete node by value from linked list', () => {
4057
const linkedList = new DoublyLinkedList();
4158

0 commit comments

Comments
 (0)