You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/codes/javascript/chapter_stack_and_queue/queue.js

30 lines
642 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* File: queue.js
* Created Time: 2022-12-04
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* 初始化队列 */
// Javascript 没有内置的队列,可以把 Array 当作队列来使用
// 注意虽然Javascript有shift()函数可以去除队首元素但是时间复杂度是O(n)的。
const queue = [];
/* 元素入队 */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
/* 访问队首元素 */
const peek = queue[0];
/* 元素出队 */
const poll = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
/* 判断队列是否为空 */
const empty = queue.length === 0;