translation: Add Python and Java code for EN version (#1345)

* Add the intial translation of code of all the languages

* test

* revert

* Remove

* Add Python and Java code for EN version
This commit is contained in:
Yudong Jin
2024-05-06 05:21:51 +08:00
committed by GitHub
parent b5e198db7d
commit 1c0f350ad6
174 changed files with 12349 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
/**
* File: array_deque.java
* Created Time: 2023-02-16
* Author: krahets (krahets@163.com), FangYuan33 (374072213@qq.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Double-ended queue class based on circular array */
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
/* Constructor */
public ArrayDeque(int capacity) {
this.nums = new int[capacity];
front = queSize = 0;
}
/* Get the capacity of the double-ended queue */
public int capacity() {
return nums.length;
}
/* Get the length of the double-ended queue */
public int size() {
return queSize;
}
/* Determine 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
return (i + capacity()) % capacity();
}
/* Front 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
front = index(front - 1);
// Add num to the front
nums[front] = num;
queSize++;
}
/* Rear 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
int rear = index(front + queSize);
// Add num to the rear
nums[rear] = num;
queSize++;
}
/* Front dequeue */
public int popFirst() {
int num = peekFirst();
// Move front pointer one position backward
front = index(front + 1);
queSize--;
return num;
}
/* Rear dequeue */
public int popLast() {
int num = peekLast();
queSize--;
return num;
}
/* Access front element */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* Access rear element */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
// Calculate rear element index
int last = index(front + queSize - 1);
return nums[last];
}
/* Return array for printing */
public int[] toArray() {
// Only convert elements within valid length range
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
}
return res;
}
}
public class array_deque {
public static void main(String[] args) {
/* Initialize 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 */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.pushLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("Element 1 enqueued at the head, 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()));
int popFirst = deque.popFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + 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);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,115 @@
/**
* File: array_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on circular array */
class ArrayQueue {
private int[] nums; // Array for storing queue elements
private int front; // Front pointer, pointing to the front element
private int queSize; // Queue length
public ArrayQueue(int capacity) {
nums = new int[capacity];
front = queSize = 0;
}
/* Get the capacity of the queue */
public int capacity() {
return nums.length;
}
/* Get the length of the queue */
public int size() {
return queSize;
}
/* Determine if the queue is empty */
public boolean isEmpty() {
return queSize == 0;
}
/* Enqueue */
public void push(int num) {
if (queSize == capacity()) {
System.out.println("Queue is full");
return;
}
// Calculate rear pointer, pointing to rear index + 1
// Use modulo operation to wrap the rear pointer from the end of the array back to the start
int rear = (front + queSize) % capacity();
// Add num to the rear
nums[rear] = num;
queSize++;
}
/* Dequeue */
public int pop() {
int num = peek();
// Move front pointer one position backward, returning to the head of the array if it exceeds the tail
front = (front + 1) % capacity();
queSize--;
return num;
}
/* Access front element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return nums[front];
}
/* Return array */
public int[] toArray() {
// Only convert elements within valid length range
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % capacity()];
}
return res;
}
}
public class array_queue {
public static void main(String[] args) {
/* Initialize queue */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* Element enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.pop();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
/* Test circular array */
for (int i = 0; i < 10; i++) {
queue.push(i);
queue.pop();
System.out.println("After the " + i + "th round of enqueueing + dequeuing, queue = " + Arrays.toString(queue.toArray()));
}
}
}

View File

@@ -0,0 +1,84 @@
/**
* File: array_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Stack class based on array */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// Initialize the list (dynamic array)
stack = new ArrayList<>();
}
/* Get the length of the stack */
public int size() {
return stack.size();
}
/* Determine if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Push */
public void push(int num) {
stack.add(num);
}
/* Pop */
public int pop() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.remove(size() - 1);
}
/* Access stack top element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
/* Convert the List to Array and return */
public Object[] toArray() {
return stack.toArray();
}
}
public class array_stack {
public static void main(String[] args) {
/* Initialize stack */
ArrayStack stack = new ArrayStack();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,46 @@
/**
* File: deque.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
Deque<Integer> deque = new LinkedList<>();
deque.offerLast(3);
deque.offerLast(2);
deque.offerLast(5);
System.out.println("Double-ended queue deque = " + deque);
/* Access element */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.offerLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + deque);
deque.offerFirst(1);
System.out.println("Element 1 enqueued at the head, deque = " + deque);
/* Element dequeue */
int popLast = deque.pollLast();
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + deque);
int popFirst = deque.pollFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + deque);
/* Get the length of the double-ended queue */
int size = deque.size();
System.out.println("Length of the double-ended queue size = " + size);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,175 @@
/**
* File: linkedlist_deque.java
* Created Time: 2023-01-20
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Double-linked list node */
class ListNode {
int val; // Node value
ListNode next; // Reference to the next node
ListNode prev; // Reference to predecessor node
ListNode(int val) {
this.val = val;
prev = next = null;
}
}
/* Double-ended queue class based on double-linked list */
class LinkedListDeque {
private ListNode front, rear; // Front node front, back node rear
private int queSize = 0; // Length of the double-ended queue
public LinkedListDeque() {
front = rear = null;
}
/* Get the length of the double-ended queue */
public int size() {
return queSize;
}
/* Determine if the double-ended queue is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Enqueue operation */
private void push(int num, boolean isFront) {
ListNode node = new ListNode(num);
// If the list is empty, make front and rear both point to node
if (isEmpty())
front = rear = node;
// Front enqueue operation
else if (isFront) {
// Add node to the head of the list
front.prev = node;
node.next = front;
front = node; // Update head node
// Rear enqueue operation
} else {
// Add node to the tail of the list
rear.next = node;
node.prev = rear;
rear = node; // Update tail node
}
queSize++; // Update queue length
}
/* Front enqueue */
public void pushFirst(int num) {
push(num, true);
}
/* Rear enqueue */
public void pushLast(int num) {
push(num, false);
}
/* Dequeue operation */
private int pop(boolean isFront) {
if (isEmpty())
throw new IndexOutOfBoundsException();
int val;
// Front dequeue operation
if (isFront) {
val = front.val; // Temporarily store the head node value
// Remove head node
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}
front = fNext; // Update head node
// Rear dequeue operation
} else {
val = rear.val; // Temporarily store the tail node value
// Remove tail node
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
}
rear = rPrev; // Update tail node
}
queSize--; // Update queue length
return val;
}
/* Front dequeue */
public int popFirst() {
return pop(true);
}
/* Rear dequeue */
public int popLast() {
return pop(false);
}
/* Access front element */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Access rear element */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return rear.val;
}
/* Return array for printing */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
LinkedListDeque deque = new LinkedListDeque();
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
/* Access element */
int peekFirst = deque.peekFirst();
System.out.println("Front element peekFirst = " + peekFirst);
int peekLast = deque.peekLast();
System.out.println("Back element peekLast = " + peekLast);
/* Element enqueue */
deque.pushLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + Arrays.toString(deque.toArray()));
deque.pushFirst(1);
System.out.println("Element 1 enqueued at the head, 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()));
int popFirst = deque.popFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + 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);
/* Determine if the double-ended queue is empty */
boolean isEmpty = deque.isEmpty();
System.out.println("Is the double-ended queue empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,104 @@
/**
* File: linkedlist_queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on linked list */
class LinkedListQueue {
private ListNode front, rear; // Front node front, back node rear
private int queSize = 0;
public LinkedListQueue() {
front = null;
rear = null;
}
/* Get the length of the queue */
public int size() {
return queSize;
}
/* Determine if the queue is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Enqueue */
public void push(int num) {
// Add num behind the tail node
ListNode node = new ListNode(num);
// If the queue is empty, make the head and tail nodes both point to that node
if (front == null) {
front = node;
rear = node;
// If the queue is not empty, add that node behind the tail node
} else {
rear.next = node;
rear = node;
}
queSize++;
}
/* Dequeue */
public int pop() {
int num = peek();
// Remove head node
front = front.next;
queSize--;
return num;
}
/* Access front element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Convert the linked list to Array and return */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_queue {
public static void main(String[] args) {
/* Initialize queue */
LinkedListQueue queue = new LinkedListQueue();
/* Element enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
queue.push(5);
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.pop();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,95 @@
/**
* File: linkedlist_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
import utils.*;
/* Stack class based on linked list */
class LinkedListStack {
private ListNode stackPeek; // Use the head node as the top of the stack
private int stkSize = 0; // Length of the stack
public LinkedListStack() {
stackPeek = null;
}
/* Get the length of the stack */
public int size() {
return stkSize;
}
/* Determine if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Push */
public void push(int num) {
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* Pop */
public int pop() {
int num = peek();
stackPeek = stackPeek.next;
stkSize--;
return num;
}
/* Access stack top element */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
/* Convert the List to Array and return */
public int[] toArray() {
ListNode node = stackPeek;
int[] res = new int[size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
public class linkedlist_stack {
public static void main(String[] args) {
/* Initialize stack */
LinkedListStack stack = new LinkedListStack();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: queue.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class queue {
public static void main(String[] args) {
/* Initialize queue */
Queue<Integer> queue = new LinkedList<>();
/* Element enqueue */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
System.out.println("Queue queue = " + queue);
/* Access front element */
int peek = queue.peek();
System.out.println("Front element peek = " + peek);
/* Element dequeue */
int pop = queue.poll();
System.out.println("Dequeued element = " + pop + ", after dequeuing" + queue);
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
/* Determine if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
public class stack {
public static void main(String[] args) {
/* Initialize stack */
Stack<Integer> stack = new Stack<>();
/* Element push */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("Stack stack = " + stack);
/* Access stack top element */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
/* Element pop */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + stack);
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
/* Determine if it's empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
}
}