๐ฆ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 };
๐็ฏไพ๏ผ
const { log } = console;
// โญ import
const { Queue } = require('../DataStructure/Queue.js');
// test Queue
function test_Queue() {
const q = new Queue(1, 2, 3);
// โ
execute commands
;[
// commands value Queue H T L | value Stack
// --------------------------------------------------------------------
`q`, // <- 1,2,3] 0 3 3 | [1,2,3 ->
`q.dequeue()`, // 1 <- 2,3] 1 3 2 | 3 [1,2
`q.first`, // 2 | 2
`q.dequeue()`, // 2 <- 3] 2 3 1 | 2 [1
`q.enqueue(4)`, // <- 3,4] 2 4 2 | [1,4
`q.dequeue()`, // 3 <- 4] 3 4 1 | 4 [1
`q.dequeue()`, // 4 <- ] 4 4 0 | 1 [
`q.dequeue()`, // null <- ] 4 4 0 | null [
`q.isEmpty`, // true | true
].forEach(cmd => {
log('-'.repeat(40));
log(`${cmd}`);
const value = eval(cmd); // execute command
if (!(value instanceof Queue)) { log(value); }
log(q.toString());
});
}
๐ ็ตๆ๏ผ
command: q
Queue:
โข visited: []
โข waiting: [1,2,3]
(H: 0, T: 3, L: 3)
----------------------------------------
command: q.dequeue()
result: 1
Queue:
โข visited: [1]
โข waiting: [2,3]
(H: 1, T: 3, L: 2)
----------------------------------------
command: q.first
result: 2
Queue:
โข visited: [1]
โข waiting: [2,3]
(H: 1, T: 3, L: 2)
----------------------------------------
command: q.dequeue()
result: 2
Queue:
โข visited: [1,2]
โข waiting: [3]
(H: 2, T: 3, L: 1)
----------------------------------------
command: q.enqueue(4)
result: undefined
Queue:
โข visited: [1,2]
โข waiting: [3,4]
(H: 2, T: 4, L: 2)
----------------------------------------
command: q.dequeue()
result: 3
Queue:
โข visited: [1,2,3]
โข waiting: [4]
(H: 3, T: 4, L: 1)
----------------------------------------
command: q.dequeue()
result: 4
Queue:
โข visited: [1,2,3,4]
โข waiting: []
(H: 4, T: 4, L: 0)
----------------------------------------
command: q.dequeue()
result: null
Queue:
โข visited: [1,2,3,4]
โข waiting: []
(H: 4, T: 4, L: 0)
----------------------------------------
command: q.isEmpty
result: true
Queue:
โข visited: [1,2,3,4]
โข waiting: []
(H: 4, T: 4, L: 0)
replit โฉ Queue (swift)
struct Queue<T>: QueueProtocol {
// backing store
var outs = [T]() // for dequeue
var ins = [T]() // for enqueue
public init() {}
public mutating func enqueue(_ element:T) {
ins.append(element)
}
// removes front of queue
@discardableResult
public mutating func dequeue() -> T? {
// if outs is empty, move items from ins to outs
if outs.isEmpty {
outs = ins.reversed()
ins.removeAll()
}
// get item from outs
return outs.popLast()
}
// queue.isEmpty
public var isEmpty: Bool {
return ins.isEmpty && outs.isEmpty
}
public var first: T? {
if !outs.isEmpty { return outs.last }
return ins.first
}
}// end: Queue
// CustomStringConvertible
extension Queue: CustomStringConvertible {
// print(queue) -> " โข 3, 2, 1 โข"
public var description: String {
return " โข [ "
+ (isEmpty ? " " : (ins.reversed() + outs).map {"\($0)"}.joined(separator: ", "))
+ " ] โข "
}
}
replit โฉ QueueProtocol (swift)
public protocol QueueProtocol: ExpressibleByArrayLiteral {
associatedtype Element
init() // default initializer
mutating func enqueue(_ element:Element)
mutating func dequeue() -> Element?
var isEmpty: Bool { get }
var first: Element? { get }
}
extension QueueProtocol {
/// initializers
// 1. var q = Queue([1,2,3])
public init(_ items:[Element]) {
self.init()
self.enqueue(items)
}
// 2. var q = Queue(1,2,3)
public init(_ items:Element...) {
self.init(items) // go to 1.
}
/// enqueue
// 4. queue.enqueue([1,2,3])
public mutating func enqueue(_ items:[Element]) {
for item in items { enqueue(item) }
}
// 5. queue.enqueue(1,2,3)
// queue.enqueue(4)
public mutating func enqueue(_ items:Element...) {
enqueue(items) // go to 4.
}
}// end: extension
// ExpressibleByArrayLiteral
extension QueueProtocol {
// 3. var q:Queue<Int> = [1,2,3]
public init(arrayLiteral items:Element...) {
self.init(items) // go to 1.
}
}
archive code
replit โฉ Queue (using object)
archived๏ผ2022.12.29
// โญ Queue
// ---------
// implementing "queue" using an "object".
// ๅ้่กๆซๆชฏใๆฝ่็ขผ็ใ๏ผ่็ขผๅฐ็ๅ
ใ
// ---------------------------------------------------------------------------
// ๐น q.enqueue(a, b, c ...)
// ๐น q.dequeue()
// ๐ธ q.first
// ๐ธ q.length
// ๐ธ q.isEmpty
// ----------------------------
// ๐น toString()
// ๐น q.log()
// ๐ธ q.debugInfo
//
class Queue {
// ๐ธ private members
#elements = Object.create(null); // pure empty object
#head = 0; // ็ฎๅๆ็ฌฌไธไฝ็็ขผ็ (already in queue)
#tail = 0; // ไธไธๅๅฏๆฝ็ๆฐ็ขผ็ (not in queue yet)
// โญ const q = new Queue(a, b, c ...)
constructor(...elements) {
this.enqueue(...elements);
}
// ๐น q.enqueue(a, b, c ...)
enqueue(...elements) {
for (const element of elements) {
this.#elements[this.#tail] = element;
this.#tail += 1;
}
}
// ๐น q.dequeue()
dequeue() {
if (this.isEmpty) return null;
const item = this.#elements[this.#head];
delete this.#elements[this.#head];
this.#head += 1;
return item;
}
// ๐ธ q.first
get first() {
if (this.isEmpty) return null;
return this.#elements[this.#head];
}
// ๐ธ q.length
get length() {
return this.#tail - this.#head;
}
// ๐ธ q.isEmpty
get isEmpty() {
return this.length === 0;
}
// ๐น toString()
toString() {
let a = [];
for(let i = this.#head; i < this.#tail; i++) {
a.push(this.#elements[i]);
}
return `Queue: [${a}]`;
}
// -------------
// dubugging
// -------------
// ๐น q.log()
log() {
console.log(this.toString());
}
// ๐ธ q.debugInfo
get debugInfo() {
return `head #: ${this.#head}, tail #: ${this.#tail}`;
}
}
module.exports = { Queue };
๐็ฏไพ๏ผ
const { log } = console;
const { Queue } = require('./DataStructure/Queue.js');
const q = new Queue(1, 2, 3);
// โ
log expressions that never throw
// ---------------------------------------------------------------------------
;[
// commands value Queue H T L
// ----------------------------------------------------------
`q`, // : [1,2,3] 0, 3, 3
`q.dequeue()`, // 1, : [2,3] 1, 3, 2
`q.first`, // 2
`q.dequeue()`, // 2, : [3] 2, 3, 1
`q.enqueue(4)`, // : [3,4] 2, 4, 2
`q.dequeue()`, // 3, : [4] 3, 4, 1
`q.dequeue()`, // 4, : [] 4, 4, 0
`q.dequeue()`, // null, : [] 4, 4, 0
`q.isEmpty`, // true,
].forEach(x => {
log('-'.repeat(40));
log(`${x}`);
x = eval(x); // execute command
if (!(x instanceof Queue)) { log(x); }
log(q.toString(), q.debugInfo);
});
Last updated