Are there better ways of modularizing the code? Does it contain things JavaScript experts would avoid? Is it idiomatic? Is there a better way of signaling an empty queue than returning null
?
function makeQueue () {
// types
let Node = {
next : null,
data : null
};
// data
let head = null;
let size = 0;
// methods
function enqueue(x) {
let newNode = {...Node, next : head, data : x};
head = newNode;
size++;
}
function dequeue() {
if (size === 0) {
return null;
}
let x = head.data;
head = head.next;
size--;
return x;
}
function getSize() {
return size;
};
// public
return {
enqueue : enqueue,
dequeue : dequeue,
getSize : getSize
};
};