📦WaitingList
data structure ⟩ WaitingList
WaitingList
is used in breadth-first search / depth-first search (Graph).
replit ⟩ WaitingList (js), require ⟩ Stack, Queue.
const { Stack } = require('./Stack.js');
const { Queue } = require('./Queue.js');
// ⭐ WaitingList
// 2022.12.28 - ??:?? - first version
// ----------------------------
// 🔹 .enqueue(a, b, c ...) // speaks in Queue's language
// 🔹 .dequeue() // (same)
// 🔸 .first
// 🔸 .length
// 🔸 .isEmpty
// ----------------------------
// 🔹 .toString()
// 🔹 .log()
// 🔸 .debugInfo
// 🔸 .isStack
// 🔸 .isQueue
//
class WaitingList {
// 🔸 private members
#data; // a stack or queue
// ⭐ constructor
constructor(mode, ...elements) {
switch (mode) {
case 'stack': this.#data = new Stack(...elements); break;
case 'queue': this.#data = new Queue(...elements); break;
default: throw new Error(`WaitingList only accepts "stack" or "queue" mode.`);
}
}
// -----------------------------
// speak in Queue's language
// -----------------------------
enqueue(...elements) { this.#data.enqueue(...elements) }
dequeue() { return this.#data.dequeue() }
get first() { return this.#data.first } // 🔸 .first
get length() { return this.#data.length } // 🔸 .length
get isEmpty() { return this.#data.isEmpty } // 🔸 .isEmpty
toString() { return this.#data.toString() } // 🔹 .toString()
// -------------
// dubugging
// -------------
log() { this.#data.log() } // 🔹 .log()
get isStack() { return this.#data instanceof Stack } // 🔸 .isStack
get isQueue() { return this.#data instanceof Queue } // 🔸 .isQueue
get debugInfo() { return this.#data.debugInfo } // 🔸 .debugInfo
}
module.exports = { WaitingList };Last updated
Was this helpful?