📦Queue
🚧 under construction
data structure ⟩ Queue
implementations
replit ⟩ WaitingList ⟩ Queue
// ⭐ Queue
// 2022.12.29 - 13.28 : add `.explored`
// ---------------------------------------------------------------------------
// 🔹 q.enqueue()
// 🔹 q.dequeue()
// 🔸 q.first
// 🔸 q.length
// 🔸 q.isEmpty
// ---------------------------------------------------------------------------
// 🔸 q.visited visited items
// 🔸 q.waiting waiting items
// 🔸 q.explored = visited + waiting
// 🔹 q.isVisited() check if item is visited
// 🔹 q.isWaiting() check if item is waiting
// 🔹 q.isExplored() check if item is explored (= visited or waiting)
// ---------------------------------------------------------------------------
// 🔹 toString()
// 🔹 q.log()
// 🔸 q.debugInfo
//
// implementing "queue" using an "array".
class Queue {
// 🔸 private members
#items = []; // explored = visited + waiting
#head = 0; // index for first waiting node in queue
// ⭐ const q = new Queue(a, b, c ...)
constructor(...items) {
this.#items.push(...items);
}
// 🔹 q.enqueue(a, b, c ...)
enqueue(...items) {
this.#items.push(...items);
}
// 🔹 q.dequeue()
dequeue() {
if (this.isEmpty) return null;
const head = this.first;
this.#head += 1;
return head;
}
// 🔸 q.first
get first() {
if (this.isEmpty) return null;
return this.#items[this.#head];
}
// 🔸 q.length
get length() {
return this.#items.length - this.#head;
}
// 🔸 q.isEmpty
get isEmpty() {
return this.length === 0;
}
// -------------
// dubugging
// -------------
// 🔹 toString()
toString() {
const str = `Queue:\n` +
` • visited: [${this.visited}]\n` +
` • waiting: [${this.waiting}]\n` +
` ${this.debugInfo}`;
return str;
}
// 🔸 q.visited
get visited() {
return this.#items.slice(0, this.#head);
}
// 🔸 q.waiting
get waiting() {
return this.#items.slice(this.#head);
}
// 🔸 q.explored
get explored() {
return this.#items.slice();
}
// 🔹 q.isVisited()
isVisited(item) {
return this.visited.includes(item);
}
// 🔹 q.isWaiting()
isWaiting(item) {
return this.waiting.includes(item);
}
// 🔹 q.isExplored()
isExplored(item) {
return this.#items.includes(item);
}
// 🔹 q.log()
log() {
console.log(this.toString());
}
// 🔸 q.debugInfo
get debugInfo() {
return `(H: ${this.#head}, T: ${this.#items.length}, L: ${this.length})`;
}
}
module.exports = { Queue };💈範例:
📃 結果:
replit ⟩ Queue (swift)
replit ⟩ QueueProtocol (swift)
archive code
replit ⟩ Queue (using object)
archived:2022.12.29
💈範例:
Last updated
Was this helpful?