循环队列(Typescript/JavaScript)

循环队列的Typescript实现

export default class RoundQueue<V> {
    get [Symbol.toStringTag]() {return 'RoundQueue'}
    [Symbol.toPrimitive](hint: string): unknown {
        if (hint === 'string') return `RoundQueue: [ ${this._queue} ]`;
        else throw new Error('RoundQueue不支持转换为 ' + hint + ' 类型');
    }
    toString() {return 'RoundQueue: ' + this._queue.toString()}

    private readonly _queue: Array<V | undefined>
    private readonly _maxNumber: number
    private _front = 0
    private _rear = 0

    /**
     * 循环队列
     * @param _queueLength, 循环队列的长度(容量)
     */
    constructor(private readonly _queueLength: number) {
        this._maxNumber = _queueLength + 1;
        this._queue = Array(this._maxNumber);
    }

    // 队列容量
    get capacity(): number {return this._queueLength}
    // 队列中当前元素的数量(队列长度)
    get length(): number {return (this._rear + this._maxNumber - this._front) % this._maxNumber}
    // 判断队列是否为空
    isEmpty(): boolean {return this._front === this._rear}
    // 判断队列是否已满
    isFull(): boolean {return (this._rear + 1) % this._maxNumber === this._front}
    // 清空队列
    clear(): void {
        this._queue.fill(undefined)
        this._front = this._rear = 0;
    }
    // 弹出一个队首元素
    popHead(): V | undefined {
        if (this.isEmpty()) return undefined;
        else {
            const el = this._queue[this._front];
            this._queue[this._front] = undefined;
            this._front = (this._front + 1) % this._maxNumber;
            return el;
        }
    }

    /**
     * 在队列尾部追加元素
     * @param element   要追加的元素
     * @param autoCover 如果队列满了, 是否自动覆盖队首元素
     */
    push(element: V, autoCover: boolean = true): void {
        if (this.isFull()) {    // 队列满
            if (autoCover) {    // 自动覆盖队头元素
                this._queue[this._front] = undefined;
                this._front = (this._front + 1) % this._maxNumber;
            } else throw new Error('RoundQueue.push Error: 队列已满,无法继续push');
        }
        this._queue[this._rear] = element;
        this._rear = (this._rear + 1) % this._maxNumber;
    }

    // 获取队首元素, 如果不存在则返回undefined
    getHead(): V | undefined {
        if (this.isEmpty()) return undefined;
        else return this._queue[this._front];
    }

    // 获取队尾元素, 如果不存在则返回undefined
    getEnd(): V | undefined {
        if (this.isEmpty()) return undefined;
        else return this._queue[(this._rear + this._maxNumber - 1) % this._maxNumber];
    }

    // 获取指定索引位置的元素, 如果不存在则返回undefined
    getElement(index: number): V {
        if (index >= this.length) throw new Error('RoundQueue.getElement Error: index索引越界');
        return this._queue[(this._front + index) % this._maxNumber] as V;
    }
}

功能测试

// 以下是 RoundQueue 的逻辑验证代码
const queue = new RoundQueue<number>(10);
for (let i = 0; i < 10; i++) {
    queue.push(i);
    console.log('queue = ', queue.toString());
    console.log('queue.getHead() = ', queue.getHead());
    console.log(`queue[${i}] = ${queue.getElement(i)}`);
    console.log('queue.getEnd() = ', queue.getEnd());
}
for (let i = 0; i < 10; i++) {
    queue.push(i * 10);
    console.log('queue = ', queue.toString());
    console.log('queue.getHead() = ', queue.getHead());
    console.log(`queue[${i}] = ${queue.getElement(i)}`);
    console.log('queue.getEnd() = ', queue.getEnd());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值