JavaScript 数据结构——队列

概念

队列是一个线性结构,特点是先进先出

入队push()

出队shift()

实现

class Queue {
    
    
    constructor() {
    
    
        this.queue = []
    }
    push(item) {
    
    
        // 入队
        this.queue.push(item)
    }
    shift() {
    
    
        // 出队
        return this.queue.shift()
    }
    getHeader() {
    
    
        // 获得队头元素
        return this.queue[0]
    }
    getLength() {
    
    
        // 获得队列长度
        return this.queue.length
    }
    isEmpty() {
    
    
        // 判断队列是否为空
        return this.getLength() === 0
    }
}

猜你喜欢

转载自blog.csdn.net/Jack_lzx/article/details/114760657