I'm trying to make a Linked list using factory functions. The defined functions inside it (like append and prepend) seem to be working fine. But they are not changing the default value of my head, tail, and size. Here's the code snippet:
const Node = (data = null, next = null) => {
return {data, next}
}
const LinkedList = () => {
let head = null;
let tail = null;
let length = 0;
// append(value) adds a new node containing value to the end of the list
const append = (value) => {
const newTailNode = Node(value);
if(!head){
return prepend(value)
}else{
let current = head;
while(current.next){
current = current.next;
}
current.next = newTailNode;
tail = newTailNode;
length++;
return tail
}
}
// prepend(value) adds a new node containing value to the start of the list
const prepend = (value) => {
// const newHeadNode = Node(value, head)
// head = newHeadNode;
// return newHeadNode
head = Node(value, head)
length++;
return {head, length}
}
// size returns the total number of nodes in the list
const size = () => {
return length
}
return {prepend, append, size,}
};
const l = LinkedList()
l.prepend(55)
l.prepend(44)
l.prepend(33)
l.append(66)
l.append(77)
console.log(l.head)
If I console.log(l.prepend(33)) it's returns {head: {data: 33, next: {data: 44, next: {data: 55, next: null}}}, length: 3}, which is absolutely fine. But if I console.log(l.head)after all these appendings and prependings, it returns null even though the head has been changed to 33 since l.prepend(33). Obviously I expect the functions to change the default value of the head, tail, and length.
headis the equivalent of a private variable - it's scope is inside the linkedlist function, it's not the same property/variable as l.head -l.headhasn't been defined.