Javascript队列和双端队列

1. 创建队列🎉

声明自己的类,表示一个队列

class Queue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }
}

::: tip

  • 首先是需要存储队列中元素,也就是上面的items,我们可以用一个数组,但是为了在获取元素的时候更加高效,选择用一个对象来存储更好。
  • count是用来控制队列的大小。
  • 有时候,我们还需要从队列前端移除元素,所以定义一个lowestCount来标记,相当于一个指针的作用。

:::

1.1 isEmpty()🌊

判断队列是否为空,

isEmpty() {
    return this.count - this.lowestCount === 0
}

1.2 size()🌊

获取队列的长度

size() {
	return this.count - this.lowestCount; 
}

1.4 enqueue() 🌊

向队列中添加元素

enqueue(element) {
    this.items[this.count] = element;
    this.count++ ;
}

1.5 dequeue() 🌊

从队列中移除元素,并返回被移除的元素

dequeue() {
    //首先判断队列是否为空
    if(this.isEmpty()) {
        return undefined;
    }
    const res = this.items[this.lowestCount];
    delete this.items[this.lowestCount];
    this.lowestCount++;
    return res
}

1.6 peek()🌊

查看队头元素

peek() {
    if(this.isEmpty()) {
        return undefined;
    }
    return this.items[this.lowestCount]
}

1.7 clear()🌊

清空队列

clear() {
    this.count = 0;
    this.items = {};
    this.lowestCount = 0;
}

1.8 toString() 🌊

如果你的items使用的是数组来定义的,那么不需要考虑这个问题

创建一个toString方法来像数组一样打印出队列中的内容

toString() {
    if(this.isEmpty()) {
        return '';
    }
    let objString = `${this.items[this.lowestCount]}`;
    for(let i = this.lowestCount+1;i<this.count;i++) {
        objString = `${objString},${this.items[i]}`;
    }
    return objString;
}

完整代码及使用

class myQueue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }
    isEmpty() {
        return this.count - this.lowestCount === 0;
    }
    size() {
        return this.count - this.lowestCount;
    }
    enqueue(element) {
        this.items[this.count] = element;
        this.count++;
    }
    dequeue() {
        if (this.isEmpty()) {
            return undefined;
        }
        const res = this.items[this.lowestCount];
        this.lowestCount++;
        return res;
    }
    peek() {
        if (this.isEmpty()) {
            return undefined;
        }
        return this.items[this.lowestCount];
    }
    clear() {
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
    }
    toString() {
        if (this.isEmpty()) {
            return '';
        }
        let objString = `${this.items[this.lowestCount]}`;
        for (let i = this.lowestCount + 1; i < this.count; i++) {
            objString = `${objString},${this.items[i]}`;
        }
        return objString;
    }
}

const queue = new myQueue()
console.log(queue.isEmpty()); //true
queue.enqueue('Ocean');
queue.enqueue('jojo');
console.log(queue.isEmpty()); //false
console.log(queue.size());  //2
console.log(queue.toString());//Ocean,jojo
console.log(queue.peek());//Ocean
console.log(queue.dequeue());//Ocean
console.log(queue.toString());//jojo
queue.clear();
console.log(queue.isEmpty());//true

2. 双端队列

允许同时从前端和后端添加和移出元素的特殊队列

在此主要整理addFront()方法

class Deque {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }
}

方法:

  • addFront(element)
    • 在前端添加新元素
  • addBack(element)
    • 在后端添加新元素(与Queue中enqueue相同)
  • removeFront()
    • 从前端移除第一个元素(与Queue中dequeue相同)
  • removeBack()
    • 从后端移除第一个元素(与Stack中的pop相同)
  • peekFront()
    • 返回队列前端的第一个元素(与Queue中的peek相同)
  • peekBack()
    • 返回队列后端的第一个元素(与Stack中的peek相同)

2.1 addFront()🌊

addFront(element) {
        if(this.isEmpty()) {
            this.addBack(element);
        }else if(this.lowestCount > 0) {
            this.lowestCount--;
            this.items[this.lowestCount] = element;
        }else {
            for(let i = this.count;i>0;i--) {
                this.items[i] = this.items[i-1]
            }
            this.count++;
            this.lowestCount = 0;
            this.items[0] = element;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值