mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 04:33:09 +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,11 +6,11 @@
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
public class ArrayDeque {
|
||||
int[] nums; // Array for storing double-ended queue elements
|
||||
int front; // Front pointer, points to the front of the queue element
|
||||
int queSize; // Double-ended queue length
|
||||
int[] nums; // Array that stores the deque elements
|
||||
int front; // Front pointer, pointing to the first element
|
||||
int queSize; // Deque size
|
||||
|
||||
/* Constructor */
|
||||
public ArrayDeque(int capacity) {
|
||||
@@ -18,73 +18,73 @@ public class ArrayDeque {
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
/* Get the deque capacity */
|
||||
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 bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
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()) {
|
||||
Console.WriteLine("Double-ended queue is full");
|
||||
Console.WriteLine("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()) {
|
||||
Console.WriteLine("Double-ended queue is full");
|
||||
Console.WriteLine("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 InvalidOperationException();
|
||||
@@ -92,19 +92,19 @@ public class ArrayDeque {
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
public int PeekLast() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
// 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
|
||||
// Copy only the 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)];
|
||||
@@ -116,37 +116,37 @@ public class ArrayDeque {
|
||||
public class array_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
ArrayDeque deque = new(10);
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Deque deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.PeekLast();
|
||||
Console.WriteLine("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("After element 4 enqueues at rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("After enqueuing element 4 at the rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("After element 1 enqueues at front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("After enqueuing element 1 at the front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int popLast = deque.PopLast();
|
||||
Console.WriteLine("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
int popFirst = deque.PopFirst();
|
||||
Console.WriteLine("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
Console.WriteLine("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,28 +6,28 @@
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
class ArrayQueue {
|
||||
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 pointer, pointing to the first element
|
||||
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 */
|
||||
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 bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
@@ -38,10 +38,10 @@ class ArrayQueue {
|
||||
Console.WriteLine("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++;
|
||||
}
|
||||
@@ -49,22 +49,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 Exception();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Return array */
|
||||
/* Return an array */
|
||||
public int[] ToArray() {
|
||||
// Elements enqueue
|
||||
// Copy only the elements within the occupied range
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % this.Capacity()];
|
||||
@@ -76,11 +76,11 @@ class ArrayQueue {
|
||||
public class array_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
@@ -88,27 +88,27 @@ public class array_queue {
|
||||
queue.Push(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
Console.WriteLine("Dequeued element pop = " + pop + ", after dequeuing, queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
Console.WriteLine("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the queue empty = " + isEmpty);
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.Push(i);
|
||||
queue.Pop();
|
||||
Console.WriteLine("Round " + i + " enqueue + dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
Console.WriteLine("Round " + i + " : after enqueue + dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,20 +6,20 @@
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
class ArrayStack {
|
||||
List<int> stack;
|
||||
public ArrayStack() {
|
||||
// Initialize list (dynamic array)
|
||||
// Initialize the list (dynamic array)
|
||||
stack = [];
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
public int Size() {
|
||||
return stack.Count;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
@@ -38,14 +38,14 @@ class ArrayStack {
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stack[Size() - 1];
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the list to an array and return it */
|
||||
public int[] ToArray() {
|
||||
return [.. stack];
|
||||
}
|
||||
@@ -54,10 +54,10 @@ class ArrayStack {
|
||||
public class array_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
ArrayStack stack = new();
|
||||
|
||||
/* 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);
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
Console.WriteLine("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack.ToArray()));
|
||||
Console.WriteLine("Popped element pop = " + pop + ", after popping, stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
Console.WriteLine("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,36 +9,36 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
public class deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Get the length of the double-ended queue */
|
||||
// In C#, use LinkedList as deque
|
||||
/* Initialize the deque */
|
||||
// In C#, use LinkedList as a deque
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.AddLast(2); // Add to the rear of the queue
|
||||
/* Enqueue elements */
|
||||
deque.AddLast(2); // Add to the rear
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // Add to the front of the queue
|
||||
deque.AddFirst(3); // Add to the front
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(",", deque));
|
||||
Console.WriteLine("Deque deque = " + string.Join(",", deque));
|
||||
|
||||
/* Update element */
|
||||
int? peekFirst = deque.First?.Value; // Rear of the queue element
|
||||
/* Access an element */
|
||||
int? peekFirst = deque.First?.Value; // Front element
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.Last?.Value; // Front of the queue element dequeues
|
||||
int? peekLast = deque.Last?.Value; // Rear element
|
||||
Console.WriteLine("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Element dequeue */
|
||||
deque.RemoveFirst(); // Check if the double-ended queue is empty
|
||||
Console.WriteLine("After front element dequeues, deque = " + string.Join(",", deque));
|
||||
deque.RemoveLast(); // Rear element dequeue
|
||||
Console.WriteLine("After rear element dequeues, deque = " + string.Join(",", deque));
|
||||
/* Dequeue an element */
|
||||
deque.RemoveFirst(); // Dequeue the front element
|
||||
Console.WriteLine("After dequeuing the front element, deque = " + string.Join(",", deque));
|
||||
deque.RemoveLast(); // Dequeue the rear element
|
||||
Console.WriteLine("After dequeuing the rear element, deque = " + string.Join(",", deque));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
Console.WriteLine("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,26 +9,26 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
/* Doubly linked list node */
|
||||
public class ListNode(int val) {
|
||||
public int val = val; // Node value
|
||||
public ListNode? next = null; // Successor node reference
|
||||
public ListNode? prev = null; // Predecessor node reference
|
||||
public ListNode? next = null; // Reference to the next node
|
||||
public ListNode? prev = null; // Reference to the previous node
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
public class LinkedListDeque {
|
||||
ListNode? front, rear; // Head node front, tail node rear
|
||||
int queSize = 0; // Length of the double-ended queue
|
||||
ListNode? front, rear; // Head node front and tail node rear
|
||||
int queSize = 0; // Deque size
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
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 bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
@@ -41,30 +41,30 @@ public class LinkedListDeque {
|
||||
front = node;
|
||||
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
|
||||
front = node; // Update the head node
|
||||
}
|
||||
// Rear of the queue enqueue operation
|
||||
// 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);
|
||||
}
|
||||
@@ -74,60 +74,60 @@ public class LinkedListDeque {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
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
|
||||
front = fNext; // Update the head node
|
||||
}
|
||||
// Temporarily store tail node value
|
||||
// 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 Exception();
|
||||
return front?.val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
public int? PeekLast() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return rear?.val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
/* Return an array for printing */
|
||||
public int?[] ToArray() {
|
||||
ListNode? node = front;
|
||||
int?[] res = new int?[Size()];
|
||||
@@ -143,37 +143,37 @@ public class LinkedListDeque {
|
||||
public class linkedlist_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Initialize the deque */
|
||||
LinkedListDeque deque = new();
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Deque deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
int? peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.PeekLast();
|
||||
Console.WriteLine("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("After element 4 enqueues at rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("After enqueuing element 4 at the rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("After element 1 enqueues at front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("After enqueuing element 1 at the front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int? popLast = deque.PopLast();
|
||||
Console.WriteLine("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Element dequeued from the rear = " + popLast + ", after dequeuing from the rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
int? popFirst = deque.PopFirst();
|
||||
Console.WriteLine("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
Console.WriteLine("Element dequeued from the front = " + popFirst + ", after dequeuing from the front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
Console.WriteLine("Deque size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the deque empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
class LinkedListQueue {
|
||||
ListNode? front, rear; // Head node front, tail node rear
|
||||
ListNode? front, rear; // Head node front and tail node rear
|
||||
int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
@@ -16,12 +16,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 bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
@@ -30,11 +30,11 @@ class LinkedListQueue {
|
||||
public void Push(int num) {
|
||||
// Add num after the tail node
|
||||
ListNode node = new(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 if (rear != null) {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
@@ -45,20 +45,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 Exception();
|
||||
return front!.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
/* Convert the linked list to an array and return it */
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return [];
|
||||
@@ -76,10 +76,10 @@ class LinkedListQueue {
|
||||
public class linkedlist_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
LinkedListQueue queue = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
@@ -87,20 +87,20 @@ public class linkedlist_queue {
|
||||
queue.Push(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
Console.WriteLine("Dequeued element pop = " + pop + ", after dequeuing, queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
Console.WriteLine("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the queue empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,21 +6,21 @@
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
class LinkedListStack {
|
||||
ListNode? stackPeek; // Use head node as stack top
|
||||
int stkSize = 0; // Stack length
|
||||
ListNode? stackPeek; // Use the head node as the stack top
|
||||
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 bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
@@ -42,14 +42,14 @@ class LinkedListStack {
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek!.val;
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
/* Convert the linked list to an array and return it */
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return [];
|
||||
@@ -67,10 +67,10 @@ class LinkedListStack {
|
||||
public class linkedlist_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
LinkedListStack stack = new();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
@@ -78,20 +78,20 @@ public class linkedlist_stack {
|
||||
stack.Push(4);
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
Console.WriteLine("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack.ToArray()));
|
||||
Console.WriteLine("Popped element pop = " + pop + ", after popping, stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
Console.WriteLine("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
public class queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
@@ -20,20 +20,20 @@ public class queue {
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
int pop = queue.Dequeue();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue));
|
||||
Console.WriteLine("Dequeued element pop = " + pop + ", after dequeuing, queue = " + string.Join(",", queue));
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
int size = queue.Count;
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
Console.WriteLine("Queue size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
bool isEmpty = queue.Count == 0;
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the queue empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,32 +9,32 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
public class stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
Stack<int> stack = new();
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
// Note: stack.ToArray() returns reversed sequence, index 0 is stack top
|
||||
// Note that stack.ToArray() returns elements in reverse insertion order, with the stack top at index 0
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack));
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
Console.WriteLine("Top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack));
|
||||
Console.WriteLine("Popped element pop = " + pop + ", after popping, stack = " + string.Join(",", stack));
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
int size = stack.Count;
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
Console.WriteLine("Stack size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
bool isEmpty = stack.Count == 0;
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
Console.WriteLine("Is the stack empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user