js数据结构中的队列和双端队列

107 阅读4分钟

队列数据结构

队列遵循先进先出(FIFO,也称为先来先服务)原则的一组有序的项。队列在尾部添加新元素,并从顶部移除元素。最新添加的元素必须在队列的末尾。 在现实中,最常见的队列的例子就是排队

创建队列

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

首先需要一个用于存储队列中元素的数据结构,
我们可以使用数组,但是为了写出一个获取元素时更高效的数据结构
,我们最好使用一个对象来存储,队列和栈的类是非常相似的,
只是添加和移除元素的原则不同

队列要实现的方法

  • enqueue: 向队列尾部添加一项
  • dequeue: 移除队列第一项
  • peek:返回队列第一个元素 -- 也是最先被添加的那个
  • isEmpty: 判断队列是否为空
  • size:队列的长度
  • clear: 清空队列
class Queue {
    construtor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    <! 和栈的push方式是没有区别的---->
    enqueue (e) {
        this.items[this.count] = e
        this.count ++
    }
    <! 对于队列而言,我们需要遵循先进先出的规则,先添加项也是先移除项---->
    dequeue () {
        <!这里可以先判断空---->
        let res = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount ++
        return res
    }
    peek () {
        <!这里可以先判断空---->
        return this.items[this.lowestCount]
    }
    isEmpty () {
        return this.count - this.lowestCount === 0
    }
    size () {
        return this.count - this.lowestCount
    }
    clear () {
        this.items = {}
        this.count = 0
        this.lowestCount = 0
    }
    toString () {
        <! 和栈一样,自行发挥,也可以查看上一篇---->
    }
}

双端队列

大家应该对双端队列这个词会有一些陌生,下面我先来解释一下什么双端队列

双端队列是一种允许我们同事从前端和后端添加和移除元素的特殊队列。 双端队列在现实生活中的例子有电影院、餐厅中排队的队伍等,举个栗子,一个刚买了票的人如果只是还需要在问一些简单的信息,就可以直接回到队伍的头部,另外,在队伍末尾的人如果赶时间,他可以直接离开队伍

创建deque类

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

既然双端队列是一种特殊的队列,我们可以看到其构造函数中的部分代码合队列相同,包括相同的内部属性和一下方法:isEempty, clear、size、toString由于双端队里允许在两端添加和移除元素,还会有下面几个方法:

在双端队列前端添加新的元素

class Deque () {
    constructor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    addFront (e) {
        if (this.isEmpty()){
            this.addBack(e)
        }
        //思路:判断队列中是否有值,如果没有直接添加。如果有,就判断low是否是大于0的,如果是,那么添加到前端,如果不是,那么就只能排队了
        else if (this.lowestCount > 0) {
            this.lowestCount --
            this.items[this.lowestCount] = e
        } else {
          for (let i = 0; i < this.count; i++) {
          
        }
        
    }
    addBack () {
        this.items[this.count] = e
        this.count ++
    }
    removeFront () {
        if (this.isEmpty()) {
            return null
        }
        let res = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount ++
        return res
    }
    removeBack () {
        if (this.isEmpty()) {
            return null
        }
        let res = this.items[this.count]
        delete this.items[this.count]
        this.count --
        return res
    }
    isEmpty () {
        return this.lowestCount - this.count === 0
    }
}

队列数据结构

队列遵循先进先出(FIFO,也称为先来先服务)原则的一组有序的项。队列在尾部添加新元素,并从顶部移除元素。最新添加的元素必须在队列的末尾。 在现实中,最常见的队列的例子就是排队

创建队列

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

首先需要一个用于存储队列中元素的数据结构,
我们可以使用数组,但是为了写出一个获取元素时更高效的数据结构
,我们最好使用一个对象来存储,队列和栈的类是非常相似的,
只是添加和移除元素的原则不同

队列要实现的方法

  • enqueue: 向队列尾部添加一项
  • dequeue: 移除队列第一项
  • peek:返回队列第一个元素 -- 也是最先被添加的那个
  • isEmpty: 判断队列是否为空
  • size:队列的长度
  • clear: 清空队列
class Queue {
    construtor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    <! 和栈的push方式是没有区别的---->
    enqueue (e) {
        this.items[this.count] = e
        this.count ++
    }
    <! 对于队列而言,我们需要遵循先进先出的规则,先添加项也是先移除项---->
    dequeue () {
        <!这里可以先判断空---->
        let res = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount ++
        return res
    }
    peek () {
        <!这里可以先判断空---->
        return this.items[this.lowestCount]
    }
    isEmpty () {
        return this.count - this.lowestCount === 0
    }
    size () {
        return this.count - this.lowestCount
    }
    clear () {
        this.items = {}
        this.count = 0
        this.lowestCount = 0
    }
    toString () {
        <! 和栈一样,自行发挥,也可以查看上一篇---->
    }
}

双端队列

大家应该对双端队列这个词会有一些陌生,下面我先来解释一下什么双端队列

双端队列是一种允许我们同事从前端和后端添加和移除元素的特殊队列。 双端队列在现实生活中的例子有电影院、餐厅中排队的队伍等,举个栗子,一个刚买了票的人如果只是还需要在问一些简单的信息,就可以直接回到队伍的头部,另外,在队伍末尾的人如果赶时间,他可以直接离开队伍

创建deque类

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

既然双端队列是一种特殊的队列,我们可以看到其构造函数中的部分代码合队列相同,包括相同的内部属性和一下方法:isEempty, clear、size、toString由于双端队里允许在两端添加和移除元素,还会有下面几个方法:

在双端队列前端添加新的元素

class Deque () {
    constructor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    addFront (e) {
        if (this.isEmpty()){
            this.addBack(e)
        }
        //思路:判断队列中是否有值,如果没有直接添加。如果有,就判断low是否是大于0的,如果是,那么添加到前端,如果不是,那么就只能排队了
        else if (this.lowestCount > 0) {
            this.lowestCount --
            this.items[this.lowestCount] = e
        } else {
          for (let i = 0; i < this.count; i++) {
          
        }
        
    }
    addBack () {
        this.items[this.count] = e
        this.count ++
    }
    removeFront () {
        if (this.isEmpty()) {
            return null
        }
        let res = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount ++
        return res
    }
    removeBack () {
        if (this.isEmpty()) {
            return null
        }
        let res = this.items[this.count]
        delete this.items[this.count]
        this.count --
        return res
    }
    isEmpty () {
        return this.lowestCount - this.count === 0
    }
}