mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
Update the chapter of stack and queue.
This commit is contained in:
@@ -34,10 +34,8 @@ class ArrayQueue {
|
||||
|
||||
/* 入队 */
|
||||
offer(num: number): void {
|
||||
if (this.size == this.capacity) {
|
||||
console.log("队列已满");
|
||||
return;
|
||||
}
|
||||
if (this.size == this.capacity)
|
||||
throw new Error("队列已满");
|
||||
// 尾结点后添加 num
|
||||
this.queue[this.rear] = num;
|
||||
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
||||
@@ -54,16 +52,15 @@ class ArrayQueue {
|
||||
|
||||
/* 访问队首元素 */
|
||||
peek(): number {
|
||||
// 删除头结点
|
||||
if (this.empty())
|
||||
throw new Error("The queue is empty!");
|
||||
throw new Error("队列为空");
|
||||
return this.queue[this.front];
|
||||
}
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
get(index: number): number {
|
||||
if (index >= this.size)
|
||||
throw new Error("Index out of bounds!");
|
||||
throw new Error("索引越界");
|
||||
return this.queue[(this.front + index) % this.capacity];
|
||||
}
|
||||
|
||||
|
||||
@@ -29,19 +29,22 @@ class ArrayStack {
|
||||
|
||||
/* 出栈 */
|
||||
pop(): number | undefined {
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top(): number | undefined {
|
||||
if (this.empty()) throw new Error('栈为空');
|
||||
if (this.empty())
|
||||
throw new Error('栈为空');
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index: number): number | undefined {
|
||||
if (index >= this.size) throw new Error('索引越界');
|
||||
if (index >= this.size)
|
||||
throw new Error('索引越界');
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ import ListNode from "../module/ListNode"
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private front: ListNode | null;
|
||||
private rear: ListNode | null; // 头结点 front ,尾结点 rear
|
||||
private front: ListNode | null; // 头结点 front
|
||||
private rear: ListNode | null; // 尾结点 rear
|
||||
private queSize: number = 0;
|
||||
|
||||
constructor() {
|
||||
@@ -46,9 +46,8 @@ class LinkedListQueue {
|
||||
/* 出队 */
|
||||
poll(): number {
|
||||
const num = this.peek();
|
||||
if (!this.front) {
|
||||
throw new Error("No element in queue!")
|
||||
}
|
||||
if (!this.front)
|
||||
throw new Error("队列为空")
|
||||
// 删除头结点
|
||||
this.front = this.front.next;
|
||||
this.queSize--;
|
||||
@@ -58,7 +57,7 @@ class LinkedListQueue {
|
||||
/* 访问队首元素 */
|
||||
peek(): number {
|
||||
if (this.size === 0)
|
||||
throw new Error("No element in queue!");
|
||||
throw new Error("队列为空");
|
||||
return this.front!.val;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
/* 初始化队列 */
|
||||
// TypeScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
// 注意:由于是数组,所以 shift() 的时间复杂度是 O(n)
|
||||
const queue: number[] = [];
|
||||
|
||||
/* 元素入队 */
|
||||
@@ -20,7 +19,7 @@ queue.push(4);
|
||||
const peek = queue[0];
|
||||
|
||||
/* 元素出队 */
|
||||
// O(n)
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
||||
Reference in New Issue
Block a user