mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Update the chapter of stack and queue.
This commit is contained in:
@ -7,13 +7,12 @@
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
#queue; // 用于存储队列元素的数组
|
||||
#front = 0; // 头指针,指向队首
|
||||
#rear = 0; // 尾指针,指向队尾 + 1
|
||||
#CAPACITY = 1e3; // 默认初始容量
|
||||
#queue; // 用于存储队列元素的数组
|
||||
#front = 0; // 头指针,指向队首
|
||||
#rear = 0; // 尾指针,指向队尾 + 1
|
||||
|
||||
constructor(capacity) {
|
||||
this.#queue = new Array(capacity ?? this.CAPACITY);
|
||||
this.#queue = new Array(capacity);
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
@ -52,7 +51,6 @@ class ArrayQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek() {
|
||||
// 删除头结点
|
||||
if (this.empty())
|
||||
throw new Error("队列为空");
|
||||
return this.#queue[this.#front];
|
||||
|
||||
@ -11,6 +11,7 @@ class ArrayStack {
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size() {
|
||||
return this.stack.length;
|
||||
@ -28,19 +29,22 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty()) throw "栈为空";
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index) {
|
||||
if (index >= this.size) throw "索引越界";
|
||||
if (index >= this.size)
|
||||
throw new Error("索引越界");
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
|
||||
@ -8,8 +8,8 @@ const ListNode = require("../include/ListNode");
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
#front;
|
||||
#rear; // 头结点 #front ,尾结点 #rear
|
||||
#front; // 头结点 #front
|
||||
#rear; // 尾结点 #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
@ -46,9 +46,6 @@ class LinkedListQueue {
|
||||
/* 出队 */
|
||||
poll() {
|
||||
const num = this.peek();
|
||||
if (!this.#front) {
|
||||
throw new Error("队列为空")
|
||||
}
|
||||
// 删除头结点
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
@ -75,6 +72,8 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
|
||||
@ -7,8 +7,7 @@
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
const queue = [];
|
||||
|
||||
/* 元素入队 */
|
||||
@ -22,7 +21,7 @@ queue.push(4);
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// shift() 方法的时间复杂度为 O(n)
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
const stack = [];
|
||||
|
||||
Reference in New Issue
Block a user