Merge pull request #139 from S-N-O-R-L-A-X/queue

Queue
This commit is contained in:
Yudong Jin
2022-12-20 21:04:28 +08:00
committed by GitHub
6 changed files with 735 additions and 1 deletions

View File

@ -428,13 +428,144 @@ comments: true
=== "JavaScript"
```js title="linkedlist_queue.js"
class LinkedListQueue {
#front;
#rear; // 头结点 #front ,尾结点 #rear
#queSize = 0;
constructor() {
this.#front = null;
this.#rear = null;
}
/* 获取队列的长度 */
get size() {
return this.#queSize;
}
/* 判断队列是否为空 */
isEmpty() {
return this.size === 0;
}
/* 入队 */
offer(num) {
// 尾结点后添加 num
const node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
if (!this.#front) {
this.#front = node;
this.#rear = node;
// 如果队列不为空,则将该结点添加到尾结点后
} else {
this.#rear.next = node;
this.#rear = node;
}
this.#queSize++;
}
/* 出队 */
poll() {
const num = this.peek();
if (!this.#front) {
throw new Error("No element in queue!")
}
// 删除头结点
this.#front = this.#front.next;
this.#queSize--;
return num;
}
/* 访问队首元素 */
peek() {
if (this.size === 0)
throw new Error("No element in queue!");
return this.#front.val;
}
/* 将链表转化为 Array 并返回 */
toArray() {
let node = this.#front;
const res = new Array(this.size);
for (let i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
```
=== "TypeScript"
```typescript title="linkedlist_queue.ts"
/* 基于链表实现的队列 */
class LinkedListQueue {
private front: ListNode | null;
private rear: ListNode | null; // 头结点 front ,尾结点 rear
private queSize: number = 0;
constructor() {
this.front = null;
this.rear = null;
}
/* 获取队列的长度 */
get size(): number {
return this.queSize;
}
/* 判断队列是否为空 */
isEmpty(): boolean {
return this.size === 0;
}
/* 入队 */
offer(num: number): void {
// 尾结点后添加 num
const node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
if (!this.front) {
this.front = node;
this.rear = node;
// 如果队列不为空,则将该结点添加到尾结点后
} else {
this.rear!.next = node;
this.rear = node;
}
this.queSize++;
}
/* 出队 */
poll(): number {
const num = this.peek();
if (!this.front) {
throw new Error("No element in queue!")
}
// 删除头结点
this.front = this.front.next;
this.queSize--;
return num;
}
/* 访问队首元素 */
peek(): number {
if (this.size === 0)
throw new Error("No element in queue!");
return this.front!.val;
}
/* 将链表转化为 Array 并返回 */
toArray(): number[] {
let node = this.front;
const res = new Array<number>(this.size);
for (let i = 0; i < res.length; i++) {
res[i] = node!.val;
node = node!.next;
}
return res;
}
}
```
=== "C"
@ -718,13 +849,160 @@ comments: true
=== "JavaScript"
```js title="array_queue.js"
/* 基于环形数组实现的队列 */
class ArrayQueue {
queue; // 用于存储队列元素的数组
front = 0; // 头指针,指向队首
rear = 0; // 尾指针,指向队尾 + 1
CAPACITY = 1e5;
constructor(capacity) {
this.queue = new Array(capacity ?? this.CAPACITY);
}
/* 获取队列的容量 */
get capacity() {
return this.queue.length;
}
/* 获取队列的长度 */
get size() {
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (this.capacity + this.rear - this.front) % this.capacity;
}
/* 判断队列是否为空 */
empty() {
return this.rear - this.front == 0;
}
/* 入队 */
offer(num) {
if (this.size == this.capacity) {
console.log("队列已满");
return;
}
// 尾结点后添加 num
this.queue[this.rear] = num;
// 尾指针向后移动一位,越过尾部后返回到数组头部
this.rear = (this.rear + 1) % this.capacity;
}
/* 出队 */
poll() {
const num = this.peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
this.front = (this.front + 1) % this.capacity;
return num;
}
/* 访问队首元素 */
peek() {
// 删除头结点
if (this.empty())
throw new Error("The queue is empty!");
return this.queue[this.front];
}
/* 访问指定索引元素 */
get(index) {
if (index >= this.size)
throw new Error("Index out of bounds!");
return this.queue[(this.front + index) % this.capacity];
}
/* 返回 Array */
toArray() {
const siz = this.size;
const cap = this.capacity;
// 仅转换有效长度范围内的列表元素
const arr = new Array(siz);
for (let i = 0, j = this.front; i < siz; i++, j++) {
arr[i] = this.queue[j % cap];
}
return arr;
}
}
```
=== "TypeScript"
```typescript title="array_queue.ts"
/* 基于环形数组实现的队列 */
class ArrayQueue {
private queue: number[]; // 用于存储队列元素的数组
private front: number = 0; // 头指针,指向队首
private rear: number = 0; // 尾指针,指向队尾 + 1
private CAPACITY: number = 1e5;
constructor(capacity?: number) {
this.queue = new Array<number>(capacity ?? this.CAPACITY);
}
/* 获取队列的容量 */
get capacity(): number {
return this.queue.length;
}
/* 获取队列的长度 */
get size(): number {
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (this.capacity + this.rear - this.front) % this.capacity;
}
/* 判断队列是否为空 */
empty(): boolean {
return this.rear - this.front == 0;
}
/* 入队 */
offer(num: number): void {
if (this.size == this.capacity) {
console.log("队列已满");
return;
}
// 尾结点后添加 num
this.queue[this.rear] = num;
// 尾指针向后移动一位,越过尾部后返回到数组头部
this.rear = (this.rear + 1) % this.capacity;
}
/* 出队 */
poll(): number {
const num = this.peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
this.front = (this.front + 1) % this.capacity;
return num;
}
/* 访问队首元素 */
peek(): number {
// 删除头结点
if (this.empty())
throw new Error("The queue is empty!");
return this.queue[this.front];
}
/* 访问指定索引元素 */
get(index: number): number {
if (index >= this.size)
throw new Error("Index out of bounds!");
return this.queue[(this.front + index) % this.capacity];
}
/* 返回 Array */
toArray(): number[] {
const siz = this.size;
const cap = this.capacity;
// 仅转换有效长度范围内的列表元素
const arr = new Array(siz);
for (let i = 0, j = this.front; i < siz; i++, j++) {
arr[i] = this.queue[j % cap];
}
return arr;
}
}
```
=== "C"