mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:16:29 +08:00
Add deque for JavaScript and typescript (#267)
* add deque in javascript and typescript * update the variables name * update the time complexity * Directly operating on the Array * fix the ts file * update the method complexity * update the js file --------- Co-authored-by: steak-zhuo <zhuoqinyue@gmail.com>
This commit is contained in:
parent
6ca5fa7d93
commit
1d1d13ec0e
2 changed files with 80 additions and 0 deletions
39
codes/javascript/chapter_stack_and_queue/deque.js
Normal file
39
codes/javascript/chapter_stack_and_queue/deque.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* File: deque.js
|
||||
* Created Time: 2023-01-17
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 初始化双向队列 */
|
||||
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
|
||||
const deque = [];
|
||||
|
||||
/* 元素入队 */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
/* unshift() 方法的时间复杂度为 O(n) */
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log("双向队列 deque = ", deque);
|
||||
|
||||
/* 访问元素 */
|
||||
const peekFirst = deque[0];
|
||||
console.log("队首元素 peekFirst = " + peekFirst);
|
||||
const peekLast = deque[deque.length - 1];
|
||||
console.log("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素出队 */
|
||||
/* shift() 方法的时间复杂度为 O(n) */
|
||||
const popFront = deque.shift();
|
||||
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
||||
const popBack = deque.pop();
|
||||
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
const size = deque.length;
|
||||
console.log("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
const isEmpty = size === 0;
|
||||
console.log("双向队列是否为空 = " + isEmpty);
|
41
codes/typescript/chapter_stack_and_queue/deque.ts
Normal file
41
codes/typescript/chapter_stack_and_queue/deque.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* File: deque.ts
|
||||
* Created Time: 2023-01-17
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 初始化双向队列 */
|
||||
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
|
||||
const deque: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
/* unshift() 方法的时间复杂度为 O(n) */
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log("双向队列 deque = ", deque);
|
||||
|
||||
/* 访问元素 */
|
||||
const peekFirst: number = deque[0];
|
||||
console.log("队首元素 peekFirst = " + peekFirst);
|
||||
const peekLast: number = deque[deque.length - 1];
|
||||
console.log("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素出队 */
|
||||
/* shift() 方法的时间复杂度为 O(n) */
|
||||
const popFront: number = deque.shift() as number;
|
||||
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
|
||||
const popBack: number = deque.pop() as number;
|
||||
console.log("队尾出队元素 popBack = " + popBack + ",队尾出队后 deque = " + deque);
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
const size: number = deque.length;
|
||||
console.log("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
const isEmpty: boolean = size === 0;
|
||||
console.log("双向队列是否为空 = " + isEmpty);
|
||||
|
||||
export {};
|
Loading…
Reference in a new issue