Update JavaScript and TypeScript codes for all chapters, rename JavaScript and TypeScript import folder to modules (#402)

* Update JavaScript and TypeScript codes

* Rename JavaScript and TypeScript import folder to modules
This commit is contained in:
Justin Tse
2023-03-03 01:34:53 +08:00
committed by GitHub
parent 7b41e6c2f0
commit e4a98bc9c5
61 changed files with 324 additions and 290 deletions

View File

@ -4,7 +4,6 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* Driver Code */
/* 初始化队列 */
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
@ -16,16 +15,21 @@ queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
console.log("队列 queue =", queue);
/* 访问队首元素 */
const peek = queue[0];
console.log("队首元素 peek =", peek);
/* 元素出队 */
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
const poll = queue.shift();
console.log("出队元素 poll =", poll, ",出队后 queue = ", queue);
/* 获取队列的长度 */
const size = queue.length;
console.log("队列长度 size =", size);
/* 判断队列是否为空 */
const empty = queue.length === 0;
const isEmpty = queue.length === 0;
console.log("队列是否为空 = ", isEmpty);