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:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions

View File

@@ -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);
}
}

View File

@@ -8,10 +8,10 @@ package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on circular array */
/* Queue based on circular array implementation */
class ArrayQueue {
private int[] nums; // Array for storing queue elements
private int front; // Front pointer, pointing to the front element
private int front; // Front pointer, points to the front of the queue element
private int queSize; // Queue length
public ArrayQueue(int capacity) {
@@ -29,7 +29,7 @@ class ArrayQueue {
return queSize;
}
/* Determine if the queue is empty */
/* Check if the queue is empty */
public boolean isEmpty() {
return queSize == 0;
}
@@ -40,10 +40,10 @@ class ArrayQueue {
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
// 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
int rear = (front + queSize) % capacity();
// Add num to the rear
// Front pointer moves one position backward
nums[rear] = num;
queSize++;
}
@@ -51,13 +51,13 @@ class ArrayQueue {
/* 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
// Move front pointer backward by one position, if it passes the tail, return to array head
front = (front + 1) % capacity();
queSize--;
return num;
}
/* Access front element */
/* Return list for printing */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
@@ -66,7 +66,7 @@ class ArrayQueue {
/* Return array */
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[j % capacity()];
@@ -77,11 +77,11 @@ class ArrayQueue {
public class array_queue {
public static void main(String[] args) {
/* Initialize queue */
/* Access front of the queue element */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* Element enqueue */
/* Elements enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -89,27 +89,27 @@ public class array_queue {
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
/* Return list for printing */
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()));
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
System.out.println("Queue length size = " + size);
/* Determine if the queue is empty */
/* Check if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
System.out.println("Queue is 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()));
System.out.println("After round " + i + " enqueue + dequeue, queue = " + Arrays.toString(queue.toArray()));
}
}
}

View File

@@ -8,12 +8,12 @@ package chapter_stack_and_queue;
import java.util.*;
/* Stack class based on array */
/* Stack based on array implementation */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// Initialize the list (dynamic array)
// Initialize list (dynamic array)
stack = new ArrayList<>();
}
@@ -22,7 +22,7 @@ class ArrayStack {
return stack.size();
}
/* Determine if the stack is empty */
/* Check if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
@@ -39,14 +39,14 @@ class ArrayStack {
return stack.remove(size() - 1);
}
/* Access stack top element */
/* Return list for printing */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
/* Convert the List to Array and return */
/* Convert List to Array and return */
public Object[] toArray() {
return stack.toArray();
}
@@ -54,10 +54,10 @@ class ArrayStack {
public class array_stack {
public static void main(String[] args) {
/* Initialize stack */
/* Access top of the stack element */
ArrayStack stack = new ArrayStack();
/* Element push */
/* Elements push onto stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -65,20 +65,20 @@ public class array_stack {
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
/* Return list for printing */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
System.out.println("Stack top element peek = " + peek);
/* Element pop */
/* Element pop from stack */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
System.out.println("Stack length size = " + size);
/* Determine if it's empty */
/* Check if empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
System.out.println("Stack is empty = " + isEmpty);
}
}

View File

@@ -10,37 +10,37 @@ import java.util.*;
public class deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
/* Get the length of the 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 */
/* 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.offerLast(4);
System.out.println("Element 4 enqueued at the tail, deque = " + deque);
System.out.println("After element 4 enqueues at rear, deque = " + deque);
deque.offerFirst(1);
System.out.println("Element 1 enqueued at the head, deque = " + deque);
System.out.println("After element 1 enqueues at front, deque = " + deque);
/* Element dequeue */
int popLast = deque.pollLast();
System.out.println("Deque tail element = " + popLast + ", after dequeuing from the tail" + deque);
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + deque);
int popFirst = deque.pollFirst();
System.out.println("Deque front element = " + popFirst + ", after dequeuing from the front" + deque);
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + deque);
/* 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);
}
}

View File

@@ -8,11 +8,11 @@ package chapter_stack_and_queue;
import java.util.*;
/* Double-linked list node */
/* Doubly linked list node */
class ListNode {
int val; // Node value
ListNode next; // Reference to successor node
ListNode prev; // Reference to predecessor node
ListNode next; // Successor node reference
ListNode prev; // Predecessor node reference
ListNode(int val) {
this.val = val;
@@ -20,9 +20,9 @@ class ListNode {
}
}
/* Double-ended queue class based on double-linked list */
/* Double-ended queue based on doubly linked list implementation */
class LinkedListDeque {
private ListNode front, rear; // Front node front, back node rear
private ListNode front, rear; // Head node front, tail node rear
private int queSize = 0; // Length of the double-ended queue
public LinkedListDeque() {
@@ -34,7 +34,7 @@ class LinkedListDeque {
return queSize;
}
/* Determine if the double-ended queue is empty */
/* Check if the double-ended queue is empty */
public boolean isEmpty() {
return size() == 0;
}
@@ -42,18 +42,18 @@ class LinkedListDeque {
/* 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 the linked list is empty, make both front and rear point to node
if (isEmpty())
front = rear = node;
// Front enqueue operation
// Front of the queue enqueue operation
else if (isFront) {
// Add node to the head of the list
// Add node to the head of the linked list
front.prev = node;
node.next = front;
front = node; // Update head node
// Rear enqueue operation
// Rear of the queue enqueue operation
} else {
// Add node to the tail of the list
// Add node to the tail of the linked list
rear.next = node;
node.prev = rear;
rear = node; // Update tail node
@@ -61,12 +61,12 @@ class LinkedListDeque {
queSize++; // Update queue length
}
/* Front enqueue */
/* Front of the queue enqueue */
public void pushFirst(int num) {
push(num, true);
}
/* Rear enqueue */
/* Rear of the queue enqueue */
public void pushLast(int num) {
push(num, false);
}
@@ -76,20 +76,20 @@ class LinkedListDeque {
if (isEmpty())
throw new IndexOutOfBoundsException();
int val;
// Front dequeue operation
// Temporarily store head node value
if (isFront) {
val = front.val; // Temporarily store the head node value
// Remove head node
val = front.val; // Delete head node
// Delete head node
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}
front = fNext; // Update head node
// Rear dequeue operation
// Temporarily store tail node value
} else {
val = rear.val; // Temporarily store the tail node value
// Remove tail node
val = rear.val; // Delete tail node
// Update tail node
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
@@ -101,24 +101,24 @@ class LinkedListDeque {
return val;
}
/* Front dequeue */
/* Rear of the queue dequeue */
public int popFirst() {
return pop(true);
}
/* Rear dequeue */
/* Access rear of the queue element */
public int popLast() {
return pop(false);
}
/* Access front element */
/* Return list for printing */
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Access rear element */
/* Driver Code */
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
@@ -139,37 +139,37 @@ class LinkedListDeque {
public class linkedlist_deque {
public static void main(String[] args) {
/* Initialize double-ended queue */
/* Get the length of the 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 */
/* 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);
}
}

View File

@@ -8,9 +8,9 @@ package chapter_stack_and_queue;
import java.util.*;
/* Queue class based on linked list */
/* Queue based on linked list implementation */
class LinkedListQueue {
private ListNode front, rear; // Front node front, back node rear
private ListNode front, rear; // Head node front, tail node rear
private int queSize = 0;
public LinkedListQueue() {
@@ -23,20 +23,20 @@ class LinkedListQueue {
return queSize;
}
/* Determine if the queue is empty */
/* Check if the queue is empty */
public boolean isEmpty() {
return size() == 0;
}
/* Enqueue */
public void push(int num) {
// Add num behind the tail node
// Add num after 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 the queue is empty, make both front and rear point to the node
if (front == null) {
front = node;
rear = node;
// If the queue is not empty, add that node behind the tail node
// If the queue is not empty, add the node after the tail node
} else {
rear.next = node;
rear = node;
@@ -47,20 +47,20 @@ class LinkedListQueue {
/* Dequeue */
public int pop() {
int num = peek();
// Remove head node
// Delete head node
front = front.next;
queSize--;
return num;
}
/* Access front element */
/* Return list for printing */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
/* Convert the linked list to Array and return */
/* Convert linked list to Array and return */
public int[] toArray() {
ListNode node = front;
int[] res = new int[size()];
@@ -74,10 +74,10 @@ class LinkedListQueue {
public class linkedlist_queue {
public static void main(String[] args) {
/* Initialize queue */
/* Access front of the queue element */
LinkedListQueue queue = new LinkedListQueue();
/* Element enqueue */
/* Elements enqueue */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -85,20 +85,20 @@ public class linkedlist_queue {
queue.push(4);
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
/* Access front element */
/* Return list for printing */
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()));
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
System.out.println("Queue length size = " + size);
/* Determine if the queue is empty */
/* Check if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
System.out.println("Queue is empty = " + isEmpty);
}
}

View File

@@ -9,10 +9,10 @@ package chapter_stack_and_queue;
import java.util.*;
import utils.*;
/* Stack class based on linked list */
/* Stack based on linked list implementation */
class LinkedListStack {
private ListNode stackPeek; // Use the head node as the top of the stack
private int stkSize = 0; // Length of the stack
private ListNode stackPeek; // Use head node as stack top
private int stkSize = 0; // Stack length
public LinkedListStack() {
stackPeek = null;
@@ -23,7 +23,7 @@ class LinkedListStack {
return stkSize;
}
/* Determine if the stack is empty */
/* Check if the stack is empty */
public boolean isEmpty() {
return size() == 0;
}
@@ -44,14 +44,14 @@ class LinkedListStack {
return num;
}
/* Access stack top element */
/* Return list for printing */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
/* Convert the List to Array and return */
/* Convert List to Array and return */
public int[] toArray() {
ListNode node = stackPeek;
int[] res = new int[size()];
@@ -65,10 +65,10 @@ class LinkedListStack {
public class linkedlist_stack {
public static void main(String[] args) {
/* Initialize stack */
/* Access top of the stack element */
LinkedListStack stack = new LinkedListStack();
/* Element push */
/* Elements push onto stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -76,20 +76,20 @@ public class linkedlist_stack {
stack.push(4);
System.out.println("Stack stack = " + Arrays.toString(stack.toArray()));
/* Access stack top element */
/* Return list for printing */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
System.out.println("Stack top element peek = " + peek);
/* Element pop */
/* Element pop from stack */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + Arrays.toString(stack.toArray()));
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
System.out.println("Stack length size = " + size);
/* Determine if it's empty */
/* Check if empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
System.out.println("Stack is empty = " + isEmpty);
}
}

View File

@@ -10,10 +10,10 @@ import java.util.*;
public class queue {
public static void main(String[] args) {
/* Initialize queue */
/* Access front of the queue element */
Queue<Integer> queue = new LinkedList<>();
/* Element enqueue */
/* Elements enqueue */
queue.offer(1);
queue.offer(3);
queue.offer(2);
@@ -21,20 +21,20 @@ public class queue {
queue.offer(4);
System.out.println("Queue queue = " + queue);
/* Access front element */
/* Return list for printing */
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);
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + queue);
/* Get the length of the queue */
int size = queue.size();
System.out.println("Length of the queue size = " + size);
System.out.println("Queue length size = " + size);
/* Determine if the queue is empty */
/* Check if the queue is empty */
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty = " + isEmpty);
System.out.println("Queue is empty = " + isEmpty);
}
}

View File

@@ -10,10 +10,10 @@ import java.util.*;
public class stack {
public static void main(String[] args) {
/* Initialize stack */
/* Access top of the stack element */
Stack<Integer> stack = new Stack<>();
/* Element push */
/* Elements push onto stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -21,20 +21,20 @@ public class stack {
stack.push(4);
System.out.println("Stack stack = " + stack);
/* Access stack top element */
/* Return list for printing */
int peek = stack.peek();
System.out.println("Top element peek = " + peek);
System.out.println("Stack top element peek = " + peek);
/* Element pop */
/* Element pop from stack */
int pop = stack.pop();
System.out.println("Popped element = " + pop + ", after popping" + stack);
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + stack);
/* Get the length of the stack */
int size = stack.size();
System.out.println("Length of the stack size = " + size);
System.out.println("Stack length size = " + size);
/* Determine if it's empty */
/* Check if empty */
boolean isEmpty = stack.isEmpty();
System.out.println("Is the stack empty = " + isEmpty);
System.out.println("Stack is empty = " + isEmpty);
}
}