mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -4,27 +4,27 @@
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue {
|
||||
#nums; // Array for storing queue elements
|
||||
#front = 0; // Front pointer, points to the front of the queue element
|
||||
#queSize = 0; // Queue length
|
||||
#nums; // Array that stores the queue elements
|
||||
#front = 0; // Front pointer, pointing to the first element
|
||||
#queSize = 0; // Queue size
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
get capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
@@ -35,10 +35,10 @@ class ArrayQueue {
|
||||
console.log('Queue is full');
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
const rear = (this.#front + this.size) % this.capacity;
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
@@ -46,21 +46,21 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
peek() {
|
||||
if (this.isEmpty()) throw new Error('Queue is empty');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* Return Array */
|
||||
/* Return an Array */
|
||||
toArray() {
|
||||
// Elements enqueue
|
||||
// Convert only the list elements within the occupied range
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
|
||||
arr[i] = this.#nums[j % this.capacity];
|
||||
@@ -69,12 +69,12 @@ class ArrayQueue {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access front of the queue element */
|
||||
/* Driver code */
|
||||
/* Initialize the queue */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
@@ -82,25 +82,25 @@ queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('Queue queue =', queue.toArray());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
const peek = queue.peek();
|
||||
console.log('Front element peek = ' + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
const pop = queue.pop();
|
||||
console.log('Dequeue element pop = ' + pop + ', after dequeue queue =', queue.toArray());
|
||||
console.log('Dequeued element pop = ' + pop + '; after dequeuing, queue =', queue.toArray());
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
const size = queue.size;
|
||||
console.log('Queue length size = ' + size);
|
||||
console.log('Queue size = ' + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('Queue is empty = ' + isEmpty);
|
||||
console.log('Is the queue empty = ' + isEmpty);
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
console.log('Round ' + i + ' rounds of enqueue + dequeue, queue =', queue.toArray());
|
||||
console.log('Round ' + i + ' : after enqueue + dequeue, queue =', queue.toArray());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user