class Node{
constructor(data){
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoubleLinkList {
constructor() {
this.size = 0;
this.head = new Node("head");
this.currNode = '';
}
isEmpty() {
return this.size === 0;
}
getLength() {
return this.size;
}
showNode() {
console.log(this.currNode.data)
}
displayList() {
let result = '';
let currNode = this.head;
while(currNode) {
result += currNode.data;
currNode = currNode.next;
if(currNode){
result += '->';
}
}
console.log(result);
}
findLast() {
let currNode = this.head;
while(currNode.next) {
currNode = currNode.next;
}
return currNode;
}
lastDisplay(){
let result = '';
let currNode = this.findLast();
while(currNode){
result += currNode.data;
currNode = currNode.prev;
if(currNode){
result += '->';
}
}
console.log(result);
}
findNode(item) {
let currNode = this.head;
while(currNode && (currNode.data !== item)){
currNode = currNode.next;
}
return currNode;
}
insertNode(item,element) {
let itemNode = this.findNode(item);
if( !itemNode ){
return;
}
let newNode = new Node(element);
newNode.next = itemNode.next;
itemNode.next = newNode;
this.size++;
}
appendNode(element) {
let currNode = this.findLast();
let newNode = new Node(element);
currNode.prev = currNode.prev;
currNode.next = newNode;
newNode.prev = currNode;
this.size++;
}
}
module.exports = DoubleLinkList;
|