class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.listNode = new Node('head');
}
insert(nodeValue, ele) {
const newNode = new Node(ele);
let indexNode = this.listNode;
while (nodeValue !== indexNode.value) {
indexNode = indexNode.next;
}
newNode.next = indexNode.next;
indexNode.next = newNode;
}
display() {
let indexNode = this.listNode;
while (indexNode) {
indexNode = indexNode.next;
console.log(indexNode)
}
}
}
var fruits = new LinkedList();
fruits.insert('head', 'Apple');
fruits.insert('Apple', 'Banana');
fruits.insert('Banana', 'Pear');
fruits.insert('Apple', '橘子');
console.log(fruits.display()); // Apple
// Banana
// Pear
console