[Algorithms] Queue & Priority Queue
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 = ;
let nextEnQueueIndex = ;
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;
}
};
}
[Algorithms] Queue & Priority Queue的更多相关文章
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
- Algorithms - Priority Queue - 优先队列
Priority queue - 优先队列 相关概念 Priority queue优先队列是一种用来维护由一组元素构成的集合S的数据结构, 其中的每一种元素都有一个相关的值,称为关键字(key). 一 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ
2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...
- 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ
许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...
- Priority Queue
优先队列 集合性质的数据类型离不开插入删除这两操作,主要区别就在于删除的时候删哪个,像栈删最晚插入的,队列删最早插入的,随机队列就随便删,而优先队列删除当前集合里最大(或最小)的元素.优先队列有很多应 ...
- STL-<queue>-priority queue的使用
简介: 优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符).它是以堆为基础实现的一种数据结构. 成员函数(Member functions) (const ...
- 优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
- Objective-C priority queue
http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...
随机推荐
- JavaScript: 2015 年回顾与展望
链接:http://www.sitepoint.com/javascript-2015-review/ JavaScript经历了一个不平凡的一年.尽管到5月份已经20年了,关于JS的新闻.项目和兴趣 ...
- [已解决] wordpress 修改 permalink 后 页面 404 问题
功能说明 为了利于SEO优化,我们需要将地址设置为永久链接,在层级不要太深的情况下实现伪静态页面的目的,例如wordpress 默认页面地址为: https://www.ryanzoe.top/?p= ...
- mac 命令行下连接到MySQL mysql: command not found
mac下刚刚安装完MySQL后使用命令连接到MySQL mysql -uroot -p 提示: -bash: mysql: command not found使用 /usr/local/mysql ...
- CM10 WIFI连不上解决方案
手机是Moto Mileston2 ,好久之前就刷成了CM10, 但是一直没出问题. 最近,发现在某些路由器上连接不上,总是 在验证账户或者获取IP. 解决办法如下: http://moto.zol. ...
- Python文件处理、函数的基本应用
可读可写: r+t:可读.可写 w+t:可写.可读with open('b.txt','w+t',encoding='utf-8') as f: print(f.readable()) p ...
- 以前刷过的FFT
Gym - 101667H 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest #include<bits/stdc++.h> using ...
- eclipse中添加svn插件
在eclipse中使用svn查看能非常方便的对代码进行查看和更新提交操作,能及时知道代码的更新状态. 在eclipse中如果要使用svn,只能使用svn插件的方式进行. 插件地址:http://sub ...
- C++之Effective STL学习笔记Item14
使用reserve来避免不必要的重新分配! The reserve member function allows you to minimize the number ofreallocations ...
- 【反省】qqxt第一场考试
我太蒟了 qwq 这是第一条 2:考试别水群,别乱fake,特别是要避免出现不顾考试时间每件事fake十分钟的情况 3:少想多写,虽然说写数据结构之前一定要先想好但是别墨迹. 4:保持对考试的敬畏,别 ...
- Java24种设计模式的优点、缺点和适用环境总结
一.7个常用的面向对象设计原则 1.单一职责原则: 它是实现高内聚.低耦合的指导方针:一个对象应该只包含单一的职责,并且该职责被完整的封装在一个类中: 2.开闭原则: 指软件实体应尽量在不改变原有的代 ...