w3resource

JavaScript Exercises: Head and Tail of a Doubly Linked Lists

JavaScript Data Structures: Exercise-4 with Solution

Write a JavaScript program to get the head and tail of a 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) {
    if (this.head === null) {
    this.head = newNode;
    this.tail = newNode;
    } 
    else
    {
    newNode.previous = this.tail;
    this.tail.next = newNode;
    this.tail = newNode;
  }

  this.length++;
   }
  
    get_Head(){
        return this.head;
     }  

  get_Tail(){
      return this.tail;
     }

 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(5));
numList.add(new Node(6));
numList.add(new Node(8));
console.log("Original Doubly Linked Lists:")
numList.printList();
console.log("Head");
ghead = numList.get_Head();
console.log(ghead);
console.log("Tail");
ghead = numList.get_Tail();
console.log(ghead);
let new_numList = new DoublyLinkedList();
console.log("Original Doubly Linked Lists:")
new_numList.printList();
console.log("Head");
ghead = new_numList.get_Head();
console.log(ghead);
console.log("Tail");
ghead = new_numList.get_Tail();
console.log(ghead);

Sample Output:

 "Original Doubly Linked Lists:"
" 2 3 5 6 8"
"Head"
[object Object] {
  next: [object Object] {
    next: [object Object] { ... },
    previous: [circular object Object],
    value: 2
  },
  previous: null,
  value: undefined
}
"Tail"
[object Object] {
  next: null,
  previous: [object Object] {
    next: [circular object Object],
    previous: [object Object] { ... },
    value: 6
  },
  value: 8
}
"Original Doubly Linked Lists:"
""
"Head"
[object Object] {
  next: null,
  previous: null,
  value: undefined
}
"Tail"
[object Object] {
  next: null,
  previous: null,
  value: undefined
} 

Flowchart:

Flowchart: JavaScript  Exercises: Count number of nodes in a Doubly Linked Lists.

Live Demo:

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


Improve this sample solution and post your code through Disqus

Linked List Previous: Count number of nodes in a Doubly Linked Lists.
Linked List Next: Insert a new node at any position of a Doubly Linked List.

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.

JavaScript: Tips of the Day

Shorten an array using its length property

A great way of shortening an array is by redefining its length property.

let array = [0, 1, 2, 3, 4, 5, 6, 6, 8, 9]
array.length = 4
// Result: [0, 1, 2, 3]

Important to know though is that this is a destructive way of changing the array. This means you lose all the other values that used to be in the array.

Ref: https://bit.ly/2LBj213