ds-algo-study
Version:
Just experimenting with publishing a package
69 lines (62 loc) • 1.35 kB
JavaScript
// ============================================================================
// Implementation Exercise: Queue
// ============================================================================
//
// -------
// Prompt:
// -------
//
// Implement a Queue and all of its methods below!
//
// ------------
// Constraints:
// ------------
//
// Make sure the time and space complexity of each is equivalent to those
// in the table provided in the Time and Space Complexity Analysis section
// of your Stack reading!
//
// -----------
// Let's Code!
// -----------
class Node {
constructor(val) {
this.value = val;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.back = null;
this.length = 0;
}
enqueue(val) {
const newNode = new Node(val);
if (!this.length) {
this.front = newNode;
this.back = newNode;
} else {
this.back.next = newNode;
this.back = newNode;
}
return ++this.length;
}
dequeue() {
if (!this.length) return null;
const removedNode = this.front;
if (this.length === 1) {
this.front = null;
this.back = null;
} else {
this.front = this.front.next;
}
this.length--;
return removedNode.value;
}
size() {
return this.length;
}
}
exports.Node = Node;
exports.Queue = Queue;