2022-12-05 14:03:36 +00:00
|
|
|
/**
|
|
|
|
* File: queue.js
|
2022-12-05 14:07:41 +00:00
|
|
|
* Created Time: 2022-12-05
|
2022-12-05 14:03:36 +00:00
|
|
|
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 初始化队列 */
|
2022-12-05 14:57:24 +00:00
|
|
|
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
2022-12-05 14:54:26 +00:00
|
|
|
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
2022-12-05 14:03:36 +00:00
|
|
|
const queue = [];
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
queue.push(1);
|
|
|
|
queue.push(3);
|
|
|
|
queue.push(2);
|
|
|
|
queue.push(5);
|
|
|
|
queue.push(4);
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
const peek = queue[0];
|
|
|
|
|
|
|
|
/* 元素出队 */
|
2022-12-05 14:54:26 +00:00
|
|
|
// O(n)
|
2022-12-05 14:03:36 +00:00
|
|
|
const poll = queue.shift();
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
const size = queue.length;
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
const empty = queue.length === 0;
|