Skip to content

Patch 5 #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/data-structures/doubly-linked-list/DoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ export default class DoublyLinkedList {
return nodes;
}

/**
* @param {DoublyLinkedListNode[]} array - Array of nodes
* @return {DoublyLinkedListNode[]}
*/
fromArray(arr = []) {
arr.forEach(node => this.append(node.value));
return this;
}

/**
* @param {function} [callback]
* @return {string}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ 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();

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', () => {
const linkedList = new DoublyLinkedList();

Expand Down