๐ฆStack
๐ง under construction
data structure โฉ Stack
implementations
replit โฉ WaitingList (js)
// โญ Stack
// 2022.12.28 - ??:?? - first version
// ----------------------------
// ๐น .push(a, b, c ...)
// ๐น .pop()
// ๐น .enqueue(a, b, c ...) // speaks in Queue's language
// ๐น .dequeue() // (same)
// ๐ธ .first
// ๐ธ .length
// ๐ธ .isEmpty
// ----------------------------
// ๐น .toString()
// ๐น .log()
//
class Stack {
// ๐ธ private members
#elements = []; // stack elements
// โญ const q = new Queue(a, b, c ...)
constructor(...elements) {
this.push(...elements);
}
// ๐น stack.push(a, b, c ...)
push(...elements) {
for (const element of elements) {
this.#elements.push(element);
}
}
// ๐น stack.pop()
pop() {
if (this.isEmpty) return null;
return this.#elements.pop();
}
// ๐ธ stack.first
get first() {
if (this.isEmpty) return null;
return this.#elements[this.length - 1];
}
// ๐ธ stack.length
get length() {
return this.#elements.length;
}
// ๐ธ stack.isEmpty
get isEmpty() {
return this.length === 0;
}
// ๐น stack.toString()
toString() {
return `Stack: [${this.#elements} ->`;
}
// -----------------------------
// speak in Queue's language
// -----------------------------
enqueue(...elements) { this.push(...elements) }
dequeue() { return this.pop() }
// -------------
// dubugging
// -------------
// ๐น .log()
log() { console.log(this.toString()) }
// ๐ธ .debugInfo
get debugInfo() { return '' }
}
module.exports = { Stack };
Last updated