Doubly Link List
Aashish Panchal
Posted on October 15, 2020
class Node {
constructor(val) {
this.val = val;
this.next = null;
this.prev = null;
}
}
class DoublylinkList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// ππ Add a new Value to the list
push(val) {
var newNode = new Node(val);
if(this.length === 0) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length++;
console.log( ` You are added ${this.length} Node ->> ${val}` );
return this;
}
// ππ Delete end value in the Node
pop() {
if(!this.head) return undefined;
var poppedNode = this.tail;
if(this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = poppedNode.prev;
}
this.length--;
console.log(` Deleted Successfully π`)
return poppedNode;
}
// ππ Delete Head Value in the list
Shift() {
if(!this.head) return undefined;
var oldHead = this.head;
if(this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.head = oldHead.next;
this.head.prev = null;
oldHead.next = null;
}
this.length--;
return oldHead;
}
// ππ Add value in the head in the list
unShift(val) {
var newNode = new Node(val);
if(this.length === 0) {
this.head = newNode;
this.tail = newNode;
} else {
this.head.prev = newNode
newNode.next = this.head;
this.head = newNode;
}
this.length++;
console.log(" -> Added Successfully π" );
return this;
}
// Write a Node number and
// Show this number Node name
get(index) {
if(index < 0 || index >= this.length) return null;
if(index <= this.length/2){
var count = 0;
var current = this.head;
while(count != index){
current = current.next;
count++;
}
return current;
} else {
var count = this.length - 1;
var current = this.tail;
while(count !== index){
current = current.next;
count++;
}
return current;
}
console.log(" -> Finded Successfully π" );
}
// Change the value in the node
set(index, val) {
var foundNode = this.get(index);
if(foundNode != null) {
foundNode.val = val;
return true;
}
return false;
}
// insert Node
insert(index, val) {
if(index < 0 || index > this.length) return false;
if(index === 0) return this.unShift(val);
if(index === this.length) return this.push(val);
var newNode = new Node(val);
var beforeNode = this.get(index - 1);
var afterNode = beforeNode.next;
beforeNode.next = newNode;
newNode.prev = beforeNode;
newNode.next = afterNode;
afterNode.prev = newNode;
this.length++;
return true;
}
// Remove a Node
remove(index) {
if(index < 0 || index >= this.length) return undefined;
if(index === 0) return this.Shift()
if(index === this.length -1) return this.pop();
var removeNode = this.get(index);
var beforeNode = removeNode.prev;
var afterNode = removeNode.next;
beforeNode.next = afterNode;
afterNode.prev = beforeNode;
// removeNode.prev.next = removeNode.next;
// removeNode.next.next = removeNode.prev;
removeNode.next = null;
removeNode.prev = null;
this.length--;
return removeNode;
}
}
var list = new DoublylinkList()
list.push("Java")
list.push("Java script")
list.push("Html")
list.push("Css")
Posted on October 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.