mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Patch 5 (#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.
This commit is contained in:
parent
7a4b829abe
commit
0ea24230d4
@ -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}
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user