Next stores attributes that point to the next node and implements js lists with objects
// The helper class generates nodes
class Node {
constructor(element) {
this.element = element;
this.next = null; }}Copy the code
Linked list operations:
- append
- getNode
- appendAt
- remove
class linkList {
constructor() {
this.size = 0;
this.head = null;
}
append(element) {
let node = new Node(element);
if (this.head == null) {
this.head = node;
} else {
// Find the last one according to the length
let current = this.getNode(this.size - 1);
current.next = node;
}
this.size++
}
getNode(index) {
if (index < 0 || index > this.size) {
throw new Error('error')}let current = this.head;
for (let i = 0; i < index; i++) {
current = current.next
}
return current
}
// Add to the specified position after the specified position
appendAt(position, elemnt) {
if (position < 0 || position >= this.size) {
throw new Error('error')}let node = new Node(elemnt);
if (position == 0) {
node.next = this.head;
this.head = node;
}
if (position < this.size) {
let current = this.getNode(position); // Find the value at the original position
let pre = this.getNode(position - 1);
pre.next = node;
let now = this.getNode(position);
console.log(current, 'current', pre, 'pre', now); node.next = current; }}/ / remove l
remove(position) {
if (position < 0 || position >= this.size) {
throw new Error('error')}let pre = this.getNode(position - 1);
let current = this.getNode(position);
pre.next = current.next
this.size--
}
// Find the specified element
indexof(element) {
let current = this.head;
for (var i = 0; i < this.size; i++) {
if (current.element == element) {
return i + 1;
}
current = current.next;
console.log(current, '1');
}
return -1}}Copy the code
Unidirectional cyclic linked lists
class Node {
constructor(element) {
this.element = element;
this.next = null; }}class CircularLinkedList {
constructor() {
this.size = 0;
this.head = null;
}
append(element) { / / insert
let node = new Node(element)
if (this.head == null) {
this.head = node;
node.next = this.head;
return;
}
let current = this.head;
while(current.next ! =this.head) { // Find the last item
current = current.next;
}
this.size++;
current.next = node;
node.next = this.head;
}
delete(position) { // Delete the specified location
if (position < 0) {
throw new Error('the error error')}if (position == 0) {
this.head = this.head.next;
return
}
let index = 0;
let current = this.head;
let pre;
while (index++ < position) {
pre = current;
current = current.next;
}
pre.next = current.next;
}
// Gets the element at the specified position
getNode(position) {
let current = this.head;
let index = 0;
while (index++ < position) { // Find the last item
current = current.next;
}
return current;
}
appendAt(position, element) { // Insert to the specified position
if (position < 0) {
throw new Error('------ error sending value ')}let index = 0;
let pre;
let node = new Node(element)
let current = this.head;
while (index++ < position) { // Find the last item
pre = current;
current = current.next;
}
pre.next = node;
node.next = current;
this.size++;
}
// Remove the specified element
deletElement(element) {
let current = this.head;
let pre;
let index = 0;
while (index < this.size) {
if (pre.element == element) {
pre.next = current.next;
this.size--
} else{ pre = current; current = current.next; index++; }}}}let node = new CircularLinkedList();
let nodes = [1.2.3.4.5];
nodes.forEach(element= > {
node.append(element);
});
console.dir(node, { depth: 100 });
// node.delete(0); // node.delete(2);
// console.log('----------------');
// console.dir(node, { depth: 100 });
// console.log('----------------', node.getNode(1));
// node.appendAt(1, 11)
node.deletElement(2)
console.dir(node, { depth: 100 });
Copy the code