DoublyLinkedList

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node{ constructor(value){ this.value = value; this.next = null; } } class Ourlist{ constructor(value){ this.head = { value: value, next: null, prev: null } this.tail = this.head; this.length = 1; } prepend(value){ //code here const newNode = new Node(value); newNode.next = this.head; this.head.prev = newNode; this.head = newNode; this.length++; return this; } append(value){ const newNode = new Node(value); newNode.prev = this.tail; this.tail.next = newNode; this.tail = newNode; this.length++; return this.head; } PrintTree(temp_head){ this.next = this.head; while(this.next !== null){ console.log(this.next); this.next = this.next.next; } } printlist(){ const array =[]; let currentNode = this.head; while(currentNode !== null){ array.push(currentNode.value); currentNode = currentNode.next; } console.log(array); return array; } insert(index,value){ const newNode = new Node(value) if(index >= this.length){ return this.append(value); } } TraverseToIndex(index){ // check index for valid one let counter = 0; let currentNode = this.head; while(counter !== index){ console.log(currentNode); currentNode = currentNode.next; counter++; if(currentNode.next == null){ return currentNode; } } return currentNode; } remove(index){ const leader = this.TraverseToIndex(index-1); const unwantedNode = leader.next; leader.next = unwantedNode.next; this.lenght--; return this.printlist(); } } const LinkedList = new Ourlist(10); let temp; LinkedList.append(16); LinkedList.append(5); LinkedList.append(22); LinkedList.append(32); LinkedList.prepend(1); LinkedList.insert(2,44); LinkedList.printlist() LinkedList.remove(4); LinkedList.printlist();
Editor Settings
Theme
Key bindings
Full width
Lines