Hi i’m following a tutorial a Linked List tutorial but i can’t access the method from the class LinkedList:
Here’s the tutorial: How to Implement a Linked List in JavaScript
Code:
// Listas atrelaçadas através de nós que se assemelham como arrays
// Criação da lista de nós a qual receberá os valores
class ListNode {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Modificador
class LinkedList {
constructor(head = null){
this.head = head;
}
size() {
let count = 0;
let node = this.head;
while(node){
count++;
node = node.next
}
return count;
}
}
// Exemplos em prática
let node1 = new ListNode(2);
let node2 = new ListNode(50);
let node3 = new ListNode(6);
node1.next = node2;
node1.next = node3;
let list = new LinkedList(node1);
// Outros métodos
console.log(list.head.next.data) //returns 5