class QNode {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
#front = null;
#reer = null;
constructor() {
this.size = 0;
}
push (x) {
const newNode = new QNode(x);
if (!this.size) {
this.#front = newNode;
this.#reer = newNode;
} else {
this.#reer.next = newNode;
this.#reer = newNode;
}
this.size++;
}
pop() {
if (this.size !== 0) {
const nextNode = this.#front.next;
const { value } = { ...this.#front };
this.#front = nextNode;
this.size--;
return value;
}
}
top() {
if (this.size !== 0) {
return this.#front.value;
}
}
}