class Node {
constructor(val) {
this
.data = val;
this
.next =
null
;
this
.prev =
null
;
}
}
class DoublyLinkedList {
constructor() {
this
.head =
null
;
this
.tail =
null
;
}
isEmpty() {
if
(
this
.head ==
null
)
return
true
;
return
false
;
}
insertEnd(val) {
let temp =
new
Node(val);
if
(
this
.head ==
null
) {
this
.head = temp;
this
.tail = temp;
}
else
{
temp.prev =
this
.tail;
this
.tail.next = temp;
this
.tail =
this
.tail.next;
}
console.log(
"inserted at end"
, val);
}
insertStart(val) {
let temp =
new
Node(val);
if
(
this
.head ==
null
) {
this
.head = temp;
this
.tail = temp;
}
else
{
temp.next =
this
.head;
this
.head.prev = temp;
this
.head = temp;
}
console.log(
"inserted at start"
, val);
}
insertAt(val, pos) {
if
(pos == 0)
return
this
.insertStart(val);
let index = pos;
let curr =
this
.head;
while
(pos > 1) {
pos -= 1;
if
(curr ===
null
)
return
console.log(
"Incorrect Position! Index does not exist."
,
pos,
curr.data
);
curr = curr.next;
}
let temp =
new
Node(val);
temp.next = curr.next;
temp.prev = curr;
curr.next.prev = temp;
curr.next = temp;
console.log(
"inserted at index"
,
index,
"value"
,
val
);
}
display() {
if
(!
this
.isEmpty()) {
let curr =
this
.head;
console.log(
"Required list is"
);
while
(curr !==
null
) {
console.log(curr.data);
curr = curr.next;
}
}
}
displayRev() {
if
(!
this
.isEmpty()) {
let curr =
this
.tail;
console.log(
"Required list in reverse order is"
);
while
(curr !==
null
) {
console.log(curr.data);
curr = curr.prev;
}
}
}
deleteStart() {
if
(
this
.head ==
null
) {
console.log(
"List is empty"
);
return
;
}
let curr =
this
.head;
this
.head =
this
.head.next;
this
.head.prev =
null
;
let delValue = curr.data;
curr =
null
;
console.log(
"Removed element: "
+ delValue);
}
deleteEnd() {
if
(
this
.head ==
null
) {
console.log(
"List is empty"
);
return
;
}
let curr =
this
.tail;
this
.tail =
this
.tail.prev;
this
.tail.next =
null
;
let delValue = curr.data;
curr =
null
;
console.log(
"Removed element: "
+ delValue);
}
deleteAt(pos) {
if
(
this
.head ==
null
) {
console.log(
"List is empty"
);
return
;
}
if
(pos == 0)
return
this
.deleteStart();
let curr =
this
.head;
while
(pos > 0) {
pos -= 1;
if
(curr ==
null
)
return
console.log(
"Incorrect Position! Index does not exist."
);
curr = curr.next;
}
if
(curr ==
this
.tail)
return
this
.deleteEnd();
let previous = curr.prev;
let next = curr.next;
previous.next = next;
next.prev = previous;
let delValue = curr.data;
curr =
null
;
console.log(
"Removed element: "
+ delValue);
}
deleteVal(val) {
if
(
this
.head ==
null
) {
console.log(
"List is empty"
);
return
;
}
if
(
this
.head.data == val)
return
this
.deleteStart();
let curr =
this
.head;
while
(curr && curr.data !== val) {
curr = curr.next;
if
(curr ==
null
)
return
console.log(
"Incorrect Value! Elements does not exist."
);
}
if
(curr ==
this
.tail)
return
this
.deleteEnd();
let previous = curr.prev;
let next = curr.next;
previous.next = next;
next.prev = previous;
let delValue = curr.data;
curr =
null
;
console.log(
"Removed element: "
+ delValue);
}
}
const dll =
new
DoublyLinkedList();
dll.insertEnd(25);
dll.insertEnd(27);
dll.insertStart(17);
dll.insertStart(29);
dll.insertAt(65, 3);
dll.display();
dll.deleteAt(3);
dll.deleteStart();
dll.deleteEnd();
dll.deleteVal(25);
dll.display();