JavaScript Exercises: Convert a Doubly Linked lists into an array
JavaScript Data Structures: Exercise-10 with Solution
Write a JavaScript program to convert a Doubly Linked lists into an array and returns it.
Sample Solution:
JavaScript Code:
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.previous = null;
}
}
class DoublyLinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
previous: null
};
this.length = 0;
this.tail = this.head;
}
add(newNode) {
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
}
else
{
newNode.previous = this.tail;
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
}
convert_to_Array() {
if(!this.head) return [];
let current = this.head;
let result = [];
while (current != null) {
result.push(current.value);
current = current.next;
}
return result;
}
printList() {
let current = this.head;
let result = [];
while (current !== null) {
result.push(current.value);
current = current.next;
}
console.log(result.join(' '));
return this;
}
}
let numList = new DoublyLinkedList();
numList.add(new Node(2));
numList.add(new Node(3));
numList.add(new Node(4));
numList.add(new Node(5));
numList.add(new Node(6));
numList.add(new Node(7));
console.log("Original Doubly Linked Lists:")
numList.printList();
console.log("Converts the said Double Linked Lists into an array");
result = numList.convert_to_Array();
console.log(result);
let numList1 = new DoublyLinkedList();
console.log("Original Doubly Linked Lists:")
numList1.printList();
console.log("Converts the said Double Linked Lists into an array");
result = numList1.convert_to_Array();
console.log(result);
Sample Output:
Original Doubly Linked Lists: 2 3 4 5 6 7 Converts the said Double Linked Lists into an array [null,2,3,4,5,6,7] Original Doubly Linked Lists: "" Converts the said Double Linked Lists into an array [null]
Flowchart:


Live Demo:
See the Pen javascript-doubly-linked-list-exercise-10 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Linked List Previous: Print a Doubly Linked lists in reverse order.
Linked List Next: Convert a Doubly Linked lists into a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Returns the average of an array, after mapping each element to a value using the provided function
Example:
const averageBy = (arr, fn) => arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) / arr.length; console.log(averageBy([{ n: 2 }, { n: 4 }, { n: 6 }, { n: 8 }], o => o.n)); console.log(averageBy([{ n: 1 }, { n: 3 }, { n: 5 }, { n: 7 }], 'n'));
Output:
5 4
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises