I am confused in this javascript linked list implementation, in the pop() function how can we do this.top.next. The next attribute is inside node class so how can we access it with a stack class attribute top?
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
this.bottom = null;
this.length = null;
}
peek() {
return this.top;
}
push(value) {
const New_node = new Node(value);
if (this.top === null) {
this.top = New_node;
this.bottom = New_node;
} else {
New_node.next = this.top;
this.top = New_node;
}
this.length++;
return this;
}
pop() {
if (this.top === null) {
return null;
} else {
this.top = this.top.next;
}
this.length--;
return this;
}
const myStack = new Stack();
myStack.push("google");
myStack.push("facebook");
myStack.push("netflix");
myStack.pop();
console.log(myStack);