mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-29 17:14:38 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -8,11 +8,11 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Double-ended queue class based on circular array */
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
class ArrayDeque {
|
||||
private int[] nums; // Array used to store elements of the double-ended queue
|
||||
private int front; // Front pointer, pointing to the front element
|
||||
private int queSize; // Length of the double-ended queue
|
||||
private int[] nums; // Array for storing double-ended queue elements
|
||||
private int front; // Front pointer, points to the front of the queue element
|
||||
private int queSize; // Double-ended queue length
|
||||
|
||||
/* Constructor */
|
||||
public ArrayDeque(int capacity) {
|
||||
@@ -30,81 +30,81 @@ class ArrayDeque {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
private int index(int i) {
|
||||
// Implement circular array by modulo operation
|
||||
// When i exceeds the tail of the array, return to the head
|
||||
// When i exceeds the head of the array, return to the tail
|
||||
// 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
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front enqueue */
|
||||
/* Front of the queue enqueue */
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
return;
|
||||
}
|
||||
// Move the front pointer one position to the left
|
||||
// Implement front crossing the head of the array to return to the tail by modulo operation
|
||||
// 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
|
||||
front = index(front - 1);
|
||||
// Add num to the front
|
||||
// Add num to front of queue
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear enqueue */
|
||||
/* Rear of the queue enqueue */
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
return;
|
||||
}
|
||||
// Calculate rear pointer, pointing to rear index + 1
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
int rear = index(front + queSize);
|
||||
// Add num to the rear
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Front dequeue */
|
||||
/* Rear of the queue dequeue */
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer one position backward
|
||||
// Move front pointer backward by one position
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Rear dequeue */
|
||||
/* Access rear of the queue element */
|
||||
public int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access front element */
|
||||
/* Return list for printing */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Access rear element */
|
||||
/* Driver Code */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Calculate rear element index
|
||||
// Initialize double-ended queue
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
public int[] toArray() {
|
||||
// Only convert elements within valid length range
|
||||
// Elements enqueue
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
@@ -115,37 +115,37 @@ class ArrayDeque {
|
||||
|
||||
public class array_deque {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize double-ended queue */
|
||||
/* Get the length of the double-ended queue */
|
||||
ArrayDeque deque = new ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Access element */
|
||||
/* Update element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Back element peekLast = " + peekLast);
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Element enqueue */
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4);
|
||||
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("Element 1 enqueued at the head, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After element 1 enqueues at front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.size();
|
||||
System.out.println("Length of the double-ended queue size = " + size);
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
|
||||
/* Determine if the double-ended queue is empty */
|
||||
/* Check if the double-ended queue is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Is the double-ended queue empty = " + isEmpty);
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user