学习javascript 数据结构 之 队列

119 阅读2分钟

概述

队列是一种特殊的线性表,它只允许你在队列的头部删除元素,在队列的末尾添加新的元素。

队列实现

定义一个类 Queue,实现以下几个函数:

  • enqueue 向队列尾部添加一个元素
  • dequeue 从队列头部删除一个元素
  • head 返回头部的元素
  • size 返回队列大小
  • clear 清空队列
  • isEmpty 判断队列是否为空
  • tail 返回队列尾节点

以数组实现队列

function Queue(){
    var items = []
    
    // 入队列
    this.enqueue = function(item){
        items.push(item)
    }
    
    // 出队列
    this.dequeue = function(){
        return items.shift()
    }
    
    // 返回队首
    this.head = function(){
        return items[0]
    }
    
    // 返回队列大小
    this.size = function(){
        return items.length
    }
    
    // 清空队列
    this.clear = function(){
        items = []
    }
    
    // 判断队列是否为空
    this.isEmpty = function(){
        items.length === 0
    }
    
    // 返回队列尾节点
    this.tail = function(){
        return items[items.length - 1]
    }
}

以链表实现队列

LinkList 链表的实现,传送门

function Queue(){
    var linklist = new LinkList();

    // 入队列
    this.enqueue = function(item){
        linklist.append(item)
    };

    // 出队列
    this.dequeue = function(){
        return linklist.removeHead()
    };

    // 返回队首
    this.head = function(){
        return linklist.head()
    };

    // 返回队尾
    this.tail = function(){
        return linklist.tail()
    };

    // 返回队列大小
    this.size = function(){
        return linklist.length()
    };

    // 清空队列
    this.clear = function(){
        linklist.clear()
    };

    // 判断队列是否为空
    this.isEmpty = function(){
        return linklist.isEmpty()
    }
}

力扣题型:

设计循环队列

循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

/**
 * @param {number} k
 */
var MyCircularQueue = function(k) {
    this.queue = Array(k+1)
    this.front = 0
    this.rear = 0
    this.max = k
}

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularQueue.prototype.enQueue = function(value) {
    if(this.isFull()) return false

    this.queue[this.rear] = value
    this.rear = (this.rear + 1) % (this.max + 1)

    return true
}

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.deQueue = function() {
    if(this.isEmpty()) return false

    this.front = (this.front + 1) % (this.max + 1)

    return true
}

/**
 * @return {number}
 */
MyCircularQueue.prototype.Front = function() {
    if(this.isEmpty()) return -1

    return this.queue[this.front]
}

/**
 * @return {number}
 */
MyCircularQueue.prototype.Rear = function() {
    if(this.isEmpty()) return -1

    return this.queue[(this.rear+this.max) % (this.max+1)]
}

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.isEmpty = function() {
    return ((this.rear-this.front+this.max+1) % (this.max + 1)) === 0
}

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.isFull = function() {
    return ((this.rear-this.front+this.max+1) % (this.max + 1)) == this.max
}

设计循环双端队列

/**
 * @param {number} k
 */
 
var MyCircularDeque = function(k) {
    this.queue = Array(k+1)
    this.front = 0
    this.rear = 0
    this.max = k
};

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertFront = function(value) {
    if(this.isFull()) return false

    this.front = (this.front + this.max) % (this.max + 1) 
    this.queue[this.front] = value
    return true
};

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertLast = function(value) {
    if(this.isFull())  return false

    this.queue[this.rear] = value
    this.rear = (this.rear + 1) % (this.max+1)

    return true
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteFront = function() {
    if(this.isEmpty())  return false

    this.front = (this.front + 1) % (this.max + 1)
    return true
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteLast = function() {
    if(this.isEmpty())  return false

    this.rear = (this.rear + this.max) % (this.max + 1)
    return true
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getFront = function() {
    if(this.isEmpty()) return -1

    return this.queue[this.front]
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
    if(this.isEmpty()) return -1

    return this.queue[(this.rear+this.max) % (this.max+1)]
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isEmpty = function() {
    return ((this.rear-this.front+this.max+1) % (this.max + 1)) === 0
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isFull = function() {
    return ((this.rear-this.front+this.max+1) % (this.max + 1)) == this.max
};

设计前中后队列