LInked List Implementation in JavaScript
https://github.com/eu81273/jsfiddle-console
by m_fil
HTML
<script src="https://cdn.jsdelivr.net/gh/eu81273/jsfiddle-console/console.js"></script>
JavaScript
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
}
addToTheEnd(value) {
let node = new Node(value);
if (this.length === 0) {
this.head = node;
} else {
let current = this.head;
while(current.next) {
current = current.next;
}
current.next = new Node(value);
}
this.length++;
}
insertInPosition(position, value) {
if (position < 0 || position > this.length) {
return 'Incorrect value of position';
}
let node = new Node(value);
if (position === 0) {
node.next = this.head;
this.head = node;
} else {
let current = this.head;
let prev = null;
let index = 0;
while (index < position) {
prev = current;
current = current.next;
index++;
}
prev.next = node;
node.next = current;
}
this.length++;
}
removeFromPosition(position) {
if (position < 0 || position > this.length) {
return 'Incorrect value of position';
}
let current = this.head;
if (position === 0) {
this.head = current.next;
} else {
let prev = null;
let index = 0;
while(index < position) {
prev = current;
current = current.next;
index++;
}
prev.next = current.next; // За левым элементом будет следовать не current, а следующий элемент за current.
}
this.length--;
return current.value;
}
removeElementByValue(value) {
return this.removeFromPosition(this.getIndexOf(value));
}
...