js实现队列结构

创建队列

let items
function Queue {
    this.enqueue = function(element){
        items.push(element)
    }
    this.dequeue = function(){
        return items.shift()
    }
    this.front = function(){
        return items[0]
    }
    this.isEmpty = function(){
        return items.length === 0
    }
    this.size = function(){
        return items.length
    }
    this.print = function(){
        console.log(items.toString())
    }
}


使用ES6改造


let Queue = (function(){
    const items = new WeakMap()
    
    class Queue {
        constructor(){
            items.set(this,[])
        }
        enqueue(element){
            let q = items.get(this)
            q.push(element)
        }
        dequeue(){
            let q = items.get(this)
            let r = q.shift()
            return r
        }
    }
    
    return Queue
})()

最小优先队列

function PriorityQueue(){
  let items = []
  
  function QueueElement(element,priority){
    this.element = element
    this.priority = priority
  }
  
  let added = false;
  this.enqueue = function(element,priority){
    let queueElement = new QueueElement(element,priority)
     
     for(let i = 0; i < items.length; i++){
       if(queueElement.priority < items[i].priority){
         items.splice(i,0,queueElement)
         added =true
         break
       }
     }
    
    if(!added){
      items.push(queueElement)
    }
  }
  
  this.print = function(){
    console.log(items)
  }
}

双端队列

class Deque {
    constructor(){
        this.count = 0;
        this.lowestCount = 0;
        this.items = {}
    }
    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.items[0] = element
        }
    }
    addBack(element){
        this.items[this.count] = element
        this.count ++
    }
    removeFront(){
        if (this.isEmpty()) {
          return undefined;
        }
        const result = this.items[this.lowestCount];
        delete this.items[this.lowestCount];
        this.lowestCount++;
        return result;
    }
    
    removeBack() {
        if (this.isEmpty()) {
          return undefined;
        }
        this.count--;
        const result = this.items[this.count];
        delete this.items[this.count];
        return result;
     }
    
    peekFront() {
        if (this.isEmpty()) {
          return undefined;
        }
        return this.items[this.lowestCount];
    }
    
    peekBack() {
        if (this.isEmpty()) {
          return undefined;
        }
        return this.items[this.count - 1];
    }
    
    isEmpty() {
        return this.size() === 0;
    }
    
    clear() {
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
    }
    
    size() {
        return this.count - this.lowestCount;
    }

    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;
    }
}

循环队列

function hotPotato(nameList,num){
    let queue = new Queue()
    
    for(let i = 0; i < nameList; i++){
        queue.enqueue(nameList[i])
    }
    
    let eliminated = '';
    
    while(queue.size() > 1){
        for(let i = 0; i < num; i++){
            queue.enqueue(queue.dequeue)
        }
        eliminated = queue.dequeue()
        console.log(eliminated + '在击鼓传花游戏中被淘汰')
    }
    
    return queue.dequeue()  
}

回文检查

function palindromeChecker(aString){
    if(
        aString === undefined ||
        aString === null ||
        (aString !== null && aString.length === 0)
    ){
        return false
    }
    
    const deque = new Deque()
    const lowerString = aString.toLocaleLowerCase().split(' ').join('')
    let firstChar;
    let lastChar;
    
    for(let i = 0; i < lowerString.length; i++){
        deque.addBack(lowerString.charAt(i))
    }
    
    while(deque.size() > 1){
        firstChar = deque.removeFront()
        lastChar = deque.removeBack();
        if(firstChar !== lastChar){
            return false
        }
    }
    
    return true
}

转载于:https://www.cnblogs.com/pluslius/p/10331661.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值