class LinkedList{
constructor(value){
this.head={
value:value,
next:null
};
this.tail=this.head;
this.length=1;
}
append(value){
const newNode={
value: value,
next: null
}
this.tail.next = newNode;
this.tail = newNode;
this.length++;
return this;
}
prepend(value){
const newNode={
value:value,
next: null
}
newNode.next=this.head;
this.head=newNode;
this.length++;
return this;
}
printList() {
const array = [];
let currentNode = this.head;
while(currentNode !== null){
array.push(currentNode.value)
currentNode = currentNode.next
}
return array;
}
insert(index,value){
if(index >= this.length){
console.log('yes')
return this.append(value);
}
const newNode = {
value: value,
next: null
}
const leader = this.traverseToIndex(index-1);
const holdingPointer = leader.next;
leader.next = newNode;
newNode.next = holdingPointer;
this.length++;
return this.printList();
}
traverseToIndex(index) {
//Check parameters
let counter = 0;
let currentNode = this.head;
while(counter !== index){
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
remove(index){
const leader=this.traverseToIndex(index-1);
const unwantedNode=leader.next;
leader.next=unwantedNode.next;
this.length--;
}
}
const myLinkedList = new LinkedList(44);
myLinkedList.append(5);
myLinkedList.append(16);
myLinkedList.append(30);
myLinkedList.prepend(12);
myLinkedList.printList();
myLinkedList.insert(2,88);
myLinkedList.insert(3,13);
myLinkedList.remove(16);
myLinkedList.printList();
console.log(myLinkedList);
class doubblyLinkedList{
constructor(value){
this.head={
value:value,
next:null,
previous:null
};
this.tail=this.head;
this.length=1;
}
append(value){
const nextNode={
value:value,
next:null,
previous:null
}
newNode.previous=this.tail;
this.tail.next=nextNode;
this.tail=newNode;
}
}
Const myLinkedList=new doubblyLinkedList(12);
myLinkedList.append(33);
myLinkedList.append(22);
}