Merge pull request #77 from S-N-O-R-L-A-X/queue
complement js and ts code in Queue
This commit is contained in:
commit
6ce8085804
30
codes/javascript/chapter_stack_and_queue/queue.js
Normal file
30
codes/javascript/chapter_stack_and_queue/queue.js
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-05
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
32
codes/typescript/chapter_stack_and_queue/queue.ts
Normal file
32
codes/typescript/chapter_stack_and_queue/queue.ts
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* File: queue.ts
|
||||
* Created Time: 2022-12-05
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 初始化队列 */
|
||||
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
|
||||
export { };
|
@ -21,7 +21,7 @@ comments: true
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| 方法 | 描述 |
|
||||
| --------- | ------------------------ |
|
||||
| --------- | ---------------------------- |
|
||||
| offer() | 元素入队,即将元素添加至队尾 |
|
||||
| poll() | 队首元素出队 |
|
||||
| front() | 访问队首元素 |
|
||||
@ -143,13 +143,59 @@ comments: true
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="queue.js"
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="queue.ts"
|
||||
/* 初始化队列 */
|
||||
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
Loading…
Reference in New Issue
Block a user