mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-02 20:01:36 +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:
@@ -6,17 +6,17 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
int *nums; // Array for storing queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Queue length
|
||||
int *nums; // Array that stores the queue elements
|
||||
int front; // Front index, identifying the first element
|
||||
int queSize; // Queue size
|
||||
int queCapacity; // Queue capacity
|
||||
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// Initialize array
|
||||
// Initialize the array
|
||||
nums = new int[capacity];
|
||||
queCapacity = capacity;
|
||||
front = queSize = 0;
|
||||
@@ -26,17 +26,17 @@ class ArrayQueue {
|
||||
delete[] nums;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
int capacity() {
|
||||
return queCapacity;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -47,10 +47,10 @@ class ArrayQueue {
|
||||
cout << "Queue is full" << endl;
|
||||
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 index, one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
int rear = (front + queSize) % queCapacity;
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
@@ -58,22 +58,22 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front index by one position, wrapping to the beginning if it passes the end
|
||||
front = (front + 1) % queCapacity;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Convert array to Vector and return */
|
||||
/* Convert the array to a vector and return it */
|
||||
vector<int> toVector() {
|
||||
// Elements enqueue
|
||||
// Copy only the elements in the occupied range
|
||||
vector<int> arr(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
arr[i] = nums[j % queCapacity];
|
||||
@@ -82,13 +82,13 @@ class ArrayQueue {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
int capacity = 10;
|
||||
ArrayQueue *queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
@@ -97,28 +97,28 @@ int main() {
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
peek = queue->pop();
|
||||
cout << "Dequeue element pop = " << peek << ", after dequeue, queue = ";
|
||||
cout << "Dequeued element pop = " << peek << "; after dequeueing, queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue->size();
|
||||
cout << "Queue length size = " << size << endl;
|
||||
cout << "Queue size = " << size << endl;
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Queue is empty = " << empty << endl;
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "After round " << i << " enqueue + dequeue, queue = ";
|
||||
cout << "Round " << i << " : after enqueue + dequeue, queue = ";
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user