博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Algorithms] Queue & Priority Queue
阅读量:5113 次
发布时间:2019-06-13

本文共 3318 字,大约阅读时间需要 11 分钟。

In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out data structure (FIFO). We can only remove items from the queue one at a time, and must remove items in the same sequence as they were placed in the queue.

Also learn how we can combine several queues to create a new data structure: a priority queue. A priority queue allows the user to add items to the queue that are deemed to be high priority, and get moved ahead in the line. This added complexity is simple to achieve and is a good example of how we can build up complexity through the use of data structures.

 

/** * * Queue * * First in First out * * API: * * enqueue() - Push a new item to the first place * dequeue() - Get first in item from the last of array * peek() - Check the next item in the queue * length * isEmpty() */function createQueue() {  const queue = [];  return {    enqueue(item) {      queue.unshift(item);    },    dequeue() {      return queue.pop();    },    peek() {      return queue[queue.length - 1];    },    get length() {      return queue.length;    },    isEmpty() {      return queue.length === 0;    },  };}/** * * Priority Queue * * First in First out for priority list and normal list * * API: * * enqueue() - Push a new item to the first place * dequeue() - Get first in item from the last of array * peek() - Check the next item in the queue * length * isEmpty() */function createPriorityQueue() {    const queue = createQueue();    const p_queue = createQueue();    return {        enqueue (item, isPriority) {            if (isPriority) {                return p_queue.enqueue(item)            }            queue.enqueue(item)        },        dequeue () {            if (!p_queue.isEmpty()) {                return p_queue.dequeue()            }            return queue.dequeue()        },        peek () {            if (!p_queue.isEmpty()) {                return p_queue.peek()            }            return queue.peek()        },        get length () {            return p_queue.length + queue.length;        },        isEmpty () {            return p_queue.isEmpty() && queue.isEmpty();        }    }}module.exports = {createQueue, createPriorityQueue};const q = createQueue();q.enqueue("Learn algorithoms");q.enqueue("Learn data structure");q.enqueue("Learn thinking");console.log(q.peek()); // 'Learn algorithoms'q.dequeue();console.log(q.peek()); // 'Learn data structure'q.dequeue();console.log(q.peek()); // 'Learn thinking'q.dequeue();console.log(q.isEmpty());const pq = createPriorityQueue()pq.enqueue('A fix here')pq.enqueue('A bug there')pq.enqueue('A new feature')pq.dequeue()pq.enqueue('Emergency task!', true)console.log(pq.dequeue())console.log(pq.peek())

 

 

Notice 'unshift' function time Complixty is not O(1). For Queue, better have enqueue and dequeue both O(1): to achieve that we can use Map:

function Queue() {  let data = new Map();  let lastDeQueueIndex = 0;  let nextEnQueueIndex = 0;  return {    enqueue(item) {      // O(1)      data.set(nextEnQueueIndex, item);      nextEnQueueIndex++;    },    dequeue() {      // O(1)      const item = data.get(lastDeQueueIndex);      lastDeQueueIndex++;      return item;    },    size() {      return nextEnQueueIndex - lastDeQueueIndex;    }  };}

 

 

 
 

转载于:https://www.cnblogs.com/Answer1215/p/10106346.html

你可能感兴趣的文章
STM32单片机使用注意事项
查看>>
swing入门教程
查看>>
好莱坞十大导演排名及其代表作,你看过多少?
查看>>
Loj #139
查看>>
hihocoder1187 Divisors
查看>>
Azure 托管镜像和非托管镜像对比
查看>>
js window.open 参数设置
查看>>
032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
查看>>
Ubuntu下安装MySQL及简单操作
查看>>
前端监控
查看>>
clipboard.js使用方法
查看>>
移动开发平台-应用之星app制作教程
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
伪类与超链接
查看>>
centos 7 redis-4.0.11 主从
查看>>
博弈论 从懵逼到入门 详解
查看>>
永远的动漫,梦想在,就有远方
查看>>
springboot No Identifier specified for entity的解决办法
查看>>
慵懒中长大的人,只会挨生活留下的耳光
查看>>
"远程桌面连接--“发生身份验证错误。要求的函数不受支持
查看>>