๐Ÿ“ฆ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)

archive code

// โญ 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