JavaScript Exercises: Create and display Doubly Linked Lists

JavaScript Data Structures: Exercise-1 with Solution

Write a JavaScript program to create and display Doubly Linked Lists.

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;

	// Insert node at end of the list
	add(newNode) {
		this.tail.next = newNode;
		newNode.previous = this.tail;
		this.tail = newNode;

    printList() {
		let current = this.head;
		let result = [];
		while (current !== null) {
			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));

Sample Output:

2 3 4 5 6 7


Flowchart: JavaScript  Exercises: Create and display doubly linked list.

Live Demo:

See the Pen javascript-doubly-linked-list-exercise-1 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

Linked List Previous: JavaScript Linked List.
Linked List Next: Count number of nodes in a Doubly Linked Lists.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.