Skip to content

Commit

Permalink
Update deque.js and deque.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Feb 4, 2023
1 parent 1d1d13e commit 7d0d3df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions codes/javascript/chapter_stack_and_queue/deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/

/* 初始化双向队列 */
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
const deque = [];

/* 元素入队 */
deque.push(2);
deque.push(5);
deque.push(4);
/* unshift() 方法的时间复杂度为 O(n) */
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
deque.unshift(3);
deque.unshift(1);
console.log("双向队列 deque = ", deque);
Expand All @@ -24,7 +24,7 @@ const peekLast = deque[deque.length - 1];
console.log("队尾元素 peekLast = " + peekLast);

/* 元素出队 */
/* shift() 方法的时间复杂度为 O(n) */
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
const popFront = deque.shift();
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
const popBack = deque.pop();
Expand All @@ -36,4 +36,4 @@ console.log("双向队列长度 size = " + size);

/* 判断双向队列是否为空 */
const isEmpty = size === 0;
console.log("双向队列是否为空 = " + isEmpty);
console.log("双向队列是否为空 = " + isEmpty);
8 changes: 4 additions & 4 deletions codes/typescript/chapter_stack_and_queue/deque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/

/* 初始化双向队列 */
/* JavaScript 没有内置的双端队列,可以把 Array 当作双端队列来使用 */
// TypeScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
const deque: number[] = [];

/* 元素入队 */
deque.push(2);
deque.push(5);
deque.push(4);
/* unshift() 方法的时间复杂度为 O(n) */
// 请注意,由于是数组,unshift() 方法的时间复杂度为 O(n)
deque.unshift(3);
deque.unshift(1);
console.log("双向队列 deque = ", deque);
Expand All @@ -24,7 +24,7 @@ const peekLast: number = deque[deque.length - 1];
console.log("队尾元素 peekLast = " + peekLast);

/* 元素出队 */
/* shift() 方法的时间复杂度为 O(n) */
// 请注意,由于是数组,shift() 方法的时间复杂度为 O(n)
const popFront: number = deque.shift() as number;
console.log("队首出队元素 popFront = " + popFront + ",队首出队后 deque = " + deque);
const popBack: number = deque.pop() as number;
Expand All @@ -38,4 +38,4 @@ console.log("双向队列长度 size = " + size);
const isEmpty: boolean = size === 0;
console.log("双向队列是否为空 = " + isEmpty);

export {};
export {};

0 comments on commit 7d0d3df

Please sign in to comment.