mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00: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:
@@ -8,11 +8,11 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
class ArrayDeque {
|
||||
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
|
||||
private int[] nums; // Array that stores the deque elements
|
||||
private int front; // Front pointer, pointing to the first element
|
||||
private int queSize; // Deque size
|
||||
|
||||
/* Constructor */
|
||||
public ArrayDeque(int capacity) {
|
||||
@@ -20,91 +20,91 @@ class ArrayDeque {
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
/* Get the deque capacity */
|
||||
public int capacity() {
|
||||
return nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
private int index(int 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 + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
System.out.println("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
|
||||
front = index(front - 1);
|
||||
// Add num to front of queue
|
||||
// Add num to the front
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("Double-ended queue is full");
|
||||
System.out.println("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
|
||||
int rear = index(front + queSize);
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
// Move front pointer backward by one position
|
||||
// Advance the front pointer by one position
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
public int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// Initialize double-ended queue
|
||||
// Calculate the index of the last element
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
public int[] toArray() {
|
||||
// Elements enqueue
|
||||
// Convert only the list elements within the occupied range
|
||||
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) {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
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()));
|
||||
System.out.println("Deque deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(4);
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After enqueuing element 4 at the rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("After element 1 enqueues at front, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After enqueuing element 1 at the front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
System.out.println("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
System.out.println("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,28 +8,28 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // Array for storing queue elements
|
||||
private int front; // Front pointer, points to the front of the queue element
|
||||
private int queSize; // Queue length
|
||||
private int[] nums; // Array that stores the queue elements
|
||||
private int front; // Front pointer, pointing to the first element
|
||||
private int queSize; // Queue size
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
/* Get the queue capacity */
|
||||
public int capacity() {
|
||||
return nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
@@ -40,10 +40,10 @@ class ArrayQueue {
|
||||
System.out.println("Queue is full");
|
||||
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 pointer, which points 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) % capacity();
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
@@ -51,22 +51,22 @@ class ArrayQueue {
|
||||
/* Dequeue */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Return array */
|
||||
/* Return an array */
|
||||
public int[] toArray() {
|
||||
// Elements enqueue
|
||||
// Convert only the list elements within the occupied range
|
||||
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) {
|
||||
/* 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);
|
||||
@@ -89,27 +89,27 @@ public class array_queue {
|
||||
queue.push(4);
|
||||
System.out.println("Queue queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.pop();
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
System.out.println("Dequeued element pop = " + pop + ", after dequeuing, queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.size();
|
||||
System.out.println("Queue length size = " + size);
|
||||
System.out.println("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
System.out.println("After round " + i + " enqueue + dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
System.out.println("Round " + i + " : after enqueue + dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,21 +8,21 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// Initialize list (dynamic array)
|
||||
// Initialize the list (dynamic array)
|
||||
stack = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
public int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -39,14 +39,14 @@ class ArrayStack {
|
||||
return stack.remove(size() - 1);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
}
|
||||
@@ -54,10 +54,10 @@ class ArrayStack {
|
||||
|
||||
public class array_stack {
|
||||
public static void main(String[] args) {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the 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()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
System.out.println("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
||||
System.out.println("Popped element pop = " + pop + ", after popping, stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.size();
|
||||
System.out.println("Stack length size = " + size);
|
||||
System.out.println("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,37 +10,37 @@ import java.util.*;
|
||||
|
||||
public class deque {
|
||||
public static void main(String[] args) {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
deque.offerLast(3);
|
||||
deque.offerLast(2);
|
||||
deque.offerLast(5);
|
||||
System.out.println("Double-ended queue deque = " + deque);
|
||||
System.out.println("Deque deque = " + deque);
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(4);
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + deque);
|
||||
System.out.println("After enqueuing element 4 at the rear, deque = " + deque);
|
||||
deque.offerFirst(1);
|
||||
System.out.println("After element 1 enqueues at front, deque = " + deque);
|
||||
System.out.println("After enqueuing element 1 at the front, deque = " + deque);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque.pollLast();
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + deque);
|
||||
System.out.println("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + deque);
|
||||
int popFirst = deque.pollFirst();
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + deque);
|
||||
System.out.println("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + deque);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
System.out.println("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
System.out.println("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.util.*;
|
||||
/* Doubly linked list node */
|
||||
class ListNode {
|
||||
int val; // Node value
|
||||
ListNode next; // Successor node reference
|
||||
ListNode prev; // Predecessor node reference
|
||||
ListNode next; // Reference to the next node
|
||||
ListNode prev; // Reference to the previous node
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
@@ -20,21 +20,21 @@ class ListNode {
|
||||
}
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
class LinkedListDeque {
|
||||
private ListNode front, rear; // Head node front, tail node rear
|
||||
private int queSize = 0; // Length of the double-ended queue
|
||||
private ListNode front, rear; // Head node front and tail node rear
|
||||
private int queSize = 0; // Deque size
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -45,28 +45,28 @@ class LinkedListDeque {
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// Front of the queue enqueue operation
|
||||
// Enqueue at the front
|
||||
else if (isFront) {
|
||||
// Add node to the head of the linked list
|
||||
front.prev = node;
|
||||
node.next = front;
|
||||
front = node; // Update head node
|
||||
// Rear of the queue enqueue operation
|
||||
front = node; // Update the head node
|
||||
// Enqueue at the rear
|
||||
} else {
|
||||
// Add node to the tail of the linked list
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // Update tail node
|
||||
rear = node; // Update the tail node
|
||||
}
|
||||
queSize++; // Update queue length
|
||||
queSize++; // Update the queue size
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
public void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
public void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
@@ -76,58 +76,58 @@ class LinkedListDeque {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
int val;
|
||||
// When the deque has one element, front and rear point to the same node
|
||||
// When the queue size is 1, the head and tail point to the same node
|
||||
if (queSize == 1) {
|
||||
val = front.val;
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
// Temporarily store head node value
|
||||
// Dequeue from the front
|
||||
else if (isFront) {
|
||||
val = front.val; // Delete head node
|
||||
// Delete head node
|
||||
val = front.val; // Save the head node value
|
||||
// Remove the head node
|
||||
ListNode fNext = front.next;
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
front = fNext; // Update head node
|
||||
// Temporarily store tail node value
|
||||
front = fNext; // Update the head node
|
||||
// Dequeue from the rear
|
||||
} else {
|
||||
val = rear.val; // Delete tail node
|
||||
// Update tail node
|
||||
val = rear.val; // Save the tail node value
|
||||
// Remove the tail node
|
||||
ListNode rPrev = rear.prev;
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
rear = rPrev; // Update tail node
|
||||
rear = rPrev; // Update the tail node
|
||||
}
|
||||
queSize--; // Update queue length
|
||||
queSize--; // Update the queue size
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
public int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
public int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
@@ -141,37 +141,37 @@ class LinkedListDeque {
|
||||
|
||||
public class linkedlist_deque {
|
||||
public static void main(String[] args) {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("Double-ended queue deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Deque deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(4);
|
||||
System.out.println("After element 4 enqueues at rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After enqueuing element 4 at the rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("After element 1 enqueues at front, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("After enqueuing element 1 at the front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + Arrays.toString(deque.toArray()));
|
||||
System.out.println("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
System.out.println("Double-ended queue length size = " + size);
|
||||
System.out.println("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("Double-ended queue is empty = " + isEmpty);
|
||||
System.out.println("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
class LinkedListQueue {
|
||||
private ListNode front, rear; // Head node front, tail node rear
|
||||
private ListNode front, rear; // Head node front and tail node rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
@@ -18,12 +18,12 @@ class LinkedListQueue {
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -32,11 +32,11 @@ class LinkedListQueue {
|
||||
public void push(int num) {
|
||||
// Add num after the tail node
|
||||
ListNode node = new ListNode(num);
|
||||
// If the queue is empty, make both front and rear point to the node
|
||||
// If the queue is empty, make both the head and tail point to this node
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
// If the queue is not empty, add this node after the tail node
|
||||
} else {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
@@ -47,20 +47,20 @@ class LinkedListQueue {
|
||||
/* Dequeue */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// Delete head node
|
||||
// Remove the head node
|
||||
front = front.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
/* Convert the linked list to an array and return it */
|
||||
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) {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
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()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.pop();
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + Arrays.toString(queue.toArray()));
|
||||
System.out.println("Dequeued element pop = " + pop + ", after dequeuing, queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.size();
|
||||
System.out.println("Queue length size = " + size);
|
||||
System.out.println("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,21 +9,21 @@ package chapter_stack_and_queue;
|
||||
import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
class LinkedListStack {
|
||||
private ListNode stackPeek; // Use head node as stack top
|
||||
private int stkSize = 0; // Stack length
|
||||
private ListNode stackPeek; // Use the head node as the stack top
|
||||
private int stkSize = 0; // Stack size
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
public int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
@@ -44,14 +44,14 @@ class LinkedListStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
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) {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the 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()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
System.out.println("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + Arrays.toString(stack.toArray()));
|
||||
System.out.println("Popped element pop = " + pop + ", after popping, stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.size();
|
||||
System.out.println("Stack length size = " + size);
|
||||
System.out.println("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import java.util.*;
|
||||
|
||||
public class queue {
|
||||
public static void main(String[] args) {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
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);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.peek();
|
||||
System.out.println("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.poll();
|
||||
System.out.println("Dequeue element pop = " + pop + ", after dequeue, queue = " + queue);
|
||||
System.out.println("Dequeued element pop = " + pop + ", after dequeuing, queue = " + queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.size();
|
||||
System.out.println("Queue length size = " + size);
|
||||
System.out.println("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("Queue is empty = " + isEmpty);
|
||||
System.out.println("Is the queue empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import java.util.*;
|
||||
|
||||
public class stack {
|
||||
public static void main(String[] args) {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the 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);
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.peek();
|
||||
System.out.println("Stack top element peek = " + peek);
|
||||
System.out.println("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.pop();
|
||||
System.out.println("Pop element pop = " + pop + ", after pop, stack = " + stack);
|
||||
System.out.println("Popped element pop = " + pop + ", after popping, stack = " + stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.size();
|
||||
System.out.println("Stack length size = " + size);
|
||||
System.out.println("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("Stack is empty = " + isEmpty);
|
||||
System.out.println("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user