Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 88bbfdc

Browse files
committed
Add fromArray() methods to LinkedList and DoublyLinkedList.
1 parent 0ea2423 commit 88bbfdc

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ export default class DoublyLinkedList {
214214
}
215215

216216
/**
217-
* @param {DoublyLinkedListNode[]} array - Array of nodes
218-
* @return {DoublyLinkedListNode[]}
217+
* @param {*[]} values - Array of values that need to be converted to linked list.
218+
* @return {DoublyLinkedList}
219219
*/
220-
fromArray(arr = []) {
221-
arr.forEach(node => this.append(node.value));
220+
fromArray(values) {
221+
values.forEach(value => this.append(value));
222+
222223
return this;
223224
}
224225

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,11 @@ 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');
39+
it('should create linked list from array', () => {
40+
const linkedList = new DoublyLinkedList();
41+
linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]);
42+
43+
expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5');
5444
});
5545

5646
it('should delete node by value from linked list', () => {

‎src/data-structures/linked-list/LinkedList.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ export default class LinkedList {
175175
return deletedHead;
176176
}
177177

178+
/**
179+
* @param {*[]} values - Array of values that need to be converted to linked list.
180+
* @return {LinkedList}
181+
*/
182+
fromArray(values) {
183+
values.forEach(value => this.append(value));
184+
185+
return this;
186+
}
187+
178188
/**
179189
* @return {LinkedListNode[]}
180190
*/

‎src/data-structures/linked-list/__test__/LinkedList.test.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ describe('LinkedList', () => {
184184
expect(linkedList.find({ callback: value => value.key === 'test5' })).toBeNull();
185185
});
186186

187+
it('should create linked list from array', () => {
188+
const linkedList = new LinkedList();
189+
linkedList.fromArray([1, 1, 2, 3, 3, 3, 4, 5]);
190+
191+
expect(linkedList.toString()).toBe('1,1,2,3,3,3,4,5');
192+
});
193+
187194
it('should find node by means of custom compare function', () => {
188195
const comparatorFunction = (a, b) => {
189196
if (a.customValue === b.customValue) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /