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:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions
@@ -4,11 +4,11 @@
* Author: Zhuo Qinyue ([email protected])
*/
/* Double-ended queue based on circular array implementation */
/* Deque implemented with a circular array */
class ArrayDeque {
#nums; // Array for storing double-ended queue elements
#front; // Front pointer, points to the front of the queue element
#queSize; // Double-ended queue length
#nums; // Array for storing deque elements
#front; // Front pointer, pointing to the first element
#queSize; // Deque size
/* Constructor */
constructor(capacity) {
@@ -17,89 +17,89 @@ class ArrayDeque {
this.#queSize = 0;
}
/* Get the capacity of the double-ended queue */
/* Get the deque capacity */
capacity() {
return this.#nums.length;
}
/* Get the length of the double-ended queue */
/* Get the deque size */
size() {
return this.#queSize;
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
isEmpty() {
return this.#queSize === 0;
}
/* Calculate circular array index */
/* Calculate an index in the circular array */
index(i) {
// Use modulo operation to wrap the array head and tail together
// When i passes the tail of the array, return to the head
// When i passes the head of the array, return to the tail
// Use modulo to connect the end of the array to its beginning
// When i passes the end of the array, wrap around to the beginning
// When i passes the beginning of the array, wrap around to the end
return (i + this.capacity()) % this.capacity();
}
/* Front of the queue enqueue */
/* Enqueue at the front */
pushFirst(num) {
if (this.#queSize === this.capacity()) {
console.log('Double-ended queue is full');
console.log('Deque is full');
return;
}
// Use modulo operation to wrap front around to the tail after passing the head of the array
// Add num to the front of the queue
// Move the front pointer one position to the left
// Use modulo to wrap front to the end when it passes the beginning of the array
this.#front = this.index(this.#front - 1);
// Add num to front of queue
// Add num to the front
this.#nums[this.#front] = num;
this.#queSize++;
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
pushLast(num) {
if (this.#queSize === this.capacity()) {
console.log('Double-ended queue is full');
console.log('Deque is full');
return;
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Calculate the rear pointer, which points one position past the last element
const rear = this.index(this.#front + this.#queSize);
// Front pointer moves one position backward
// Add num to the rear
this.#nums[rear] = num;
this.#queSize++;
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
popFirst() {
const num = this.peekFirst();
// Move front pointer backward by one position
// Advance the front pointer by one position
this.#front = this.index(this.#front + 1);
this.#queSize--;
return num;
}
/* Access rear of the queue element */
/* Dequeue from the rear */
popLast() {
const num = this.peekLast();
this.#queSize--;
return num;
}
/* Return list for printing */
/* Access the front element */
peekFirst() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
if (this.isEmpty()) throw new Error('Deque is empty');
return this.#nums[this.#front];
}
/* Driver Code */
/* Access the rear element */
peekLast() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
// Initialize double-ended queue
if (this.isEmpty()) throw new Error('Deque is empty');
// Calculate the index of the last element
const last = this.index(this.#front + this.#queSize - 1);
return this.#nums[last];
}
/* Return array for printing */
/* Return an array for printing */
toArray() {
// Elements enqueue
// Convert only the list elements within the occupied range
const res = [];
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
res[i] = this.#nums[this.index(j)];
@@ -108,8 +108,8 @@ class ArrayDeque {
}
}
/* Driver Code */
/* Get the length of the double-ended queue */
/* Driver code */
/* Initialize the deque */
const capacity = 5;
const deque = new ArrayDeque(capacity);
deque.pushLast(3);
@@ -117,40 +117,40 @@ deque.pushLast(2);
deque.pushLast(5);
console.log('Deque deque = [' + deque.toArray() + ']');
/* Update element */
/* Access an element */
const peekFirst = deque.peekFirst();
console.log('Front element peekFirst = ' + peekFirst);
const peekLast = deque.peekLast();
console.log('Rear element peekLast = ' + peekLast);
/* Elements enqueue */
/* Enqueue elements */
deque.pushLast(4);
console.log('After element 4 enqueues at rear, deque = [' + deque.toArray() + ']');
console.log('After enqueuing 4 at the rear, deque = [' + deque.toArray() + ']');
deque.pushFirst(1);
console.log('After element 1 enqueues at front, deque = [' + deque.toArray() + ']');
console.log('After enqueuing 1 at the front, deque = [' + deque.toArray() + ']');
/* Element dequeue */
/* Dequeue an element */
const popLast = deque.popLast();
console.log(
'Rear dequeue element = ' +
'Element dequeued from the rear = ' +
popLast +
', after rear dequeue deque = [' +
'; after removing from the rear, deque = [' +
deque.toArray() +
']'
);
const popFirst = deque.popFirst();
console.log(
'Front dequeue element = ' +
'Element dequeued from the front = ' +
popFirst +
', after front dequeue deque = [' +
'; after removing from the front, deque = [' +
deque.toArray() +
']'
);
/* Get the length of the double-ended queue */
/* Get the deque size */
const size = deque.size();
console.log('Double-ended queue length size = ' + size);
console.log('Deque size = ' + size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
const isEmpty = deque.isEmpty();
console.log('Double-ended queue is empty = ' + isEmpty);
console.log('Is the deque empty = ' + isEmpty);