mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
152
en/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
152
en/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* File: array_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
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
|
||||
|
||||
/* Constructor */
|
||||
public ArrayDeque(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
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
|
||||
return (i + Capacity()) % Capacity();
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
public void PushFirst(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("Double-ended queue 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
|
||||
front = Index(front - 1);
|
||||
// Add num to front of queue
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
public void PushLast(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("Double-ended queue is full");
|
||||
return;
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
int rear = Index(front + queSize);
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
public int PopFirst() {
|
||||
int num = PeekFirst();
|
||||
// Move front pointer backward by one position
|
||||
front = Index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
public int PopLast() {
|
||||
int num = PeekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int PeekFirst() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public int PeekLast() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
// Initialize double-ended queue
|
||||
int last = Index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
public int[] ToArray() {
|
||||
// Elements enqueue
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[Index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Get the length of the double-ended queue */
|
||||
ArrayDeque deque = new(10);
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Update element */
|
||||
int peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int peekLast = deque.PeekLast();
|
||||
Console.WriteLine("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("After element 4 enqueues at rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("After element 1 enqueues at front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
int popLast = deque.PopLast();
|
||||
Console.WriteLine("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
int popFirst = deque.PopFirst();
|
||||
Console.WriteLine("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
114
en/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
114
en/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: array_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
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
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
public void Push(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
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
|
||||
int rear = (front + queSize) % Capacity();
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
front = (front + 1) % Capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* Return array */
|
||||
public int[] ToArray() {
|
||||
// Elements enqueue
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % this.Capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
|
||||
/* Test 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
84
en/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
84
en/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* File: array_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
List<int> stack;
|
||||
public ArrayStack() {
|
||||
// Initialize list (dynamic array)
|
||||
stack = [];
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
public int Size() {
|
||||
return stack.Count;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
public void Push(int num) {
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
public int Pop() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
var val = Peek();
|
||||
stack.RemoveAt(Size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stack[Size() - 1];
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
public int[] ToArray() {
|
||||
return [.. stack];
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
ArrayStack stack = new();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
44
en/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
44
en/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.cs
|
||||
* Created Time: 2022-12-30
|
||||
* Author: moonache (microin1301@outlook.com)
|
||||
*/
|
||||
|
||||
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
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.AddLast(2); // Add to the rear of the queue
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // Add to the front of the queue
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(",", deque));
|
||||
|
||||
/* Update element */
|
||||
int? peekFirst = deque.First?.Value; // Rear of the queue element
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.Last?.Value; // Front of the queue element dequeues
|
||||
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));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
177
en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
177
en/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* File: linkedlist_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
public class LinkedListDeque {
|
||||
ListNode? front, rear; // Head node front, tail node rear
|
||||
int queSize = 0; // Length of the double-ended queue
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue operation */
|
||||
void Push(int num, bool isFront) {
|
||||
ListNode node = new(num);
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (IsEmpty()) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
// Front of the queue enqueue operation
|
||||
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
|
||||
else {
|
||||
// Add node to the tail of the linked list
|
||||
rear!.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // Update tail node
|
||||
}
|
||||
|
||||
queSize++; // Update queue length
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
public void PushFirst(int num) {
|
||||
Push(num, true);
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
public void PushLast(int num) {
|
||||
Push(num, false);
|
||||
}
|
||||
|
||||
/* Dequeue operation */
|
||||
int? Pop(bool isFront) {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
int? val;
|
||||
// Temporarily store head node value
|
||||
if (isFront) {
|
||||
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
|
||||
}
|
||||
// Temporarily store tail node value
|
||||
else {
|
||||
val = rear?.val; // Delete tail node
|
||||
// Update tail node
|
||||
ListNode? rPrev = rear?.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear!.prev = null;
|
||||
}
|
||||
rear = rPrev; // Update tail node
|
||||
}
|
||||
|
||||
queSize--; // Update queue length
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
public int? PopFirst() {
|
||||
return Pop(true);
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
public int? PopLast() {
|
||||
return Pop(false);
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int? PeekFirst() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front?.val;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public int? PeekLast() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return rear?.val;
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
public int?[] ToArray() {
|
||||
ListNode? node = front;
|
||||
int?[] res = new int?[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node?.val;
|
||||
node = node?.next;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Get the length of the double-ended queue */
|
||||
LinkedListDeque deque = new();
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("Double-ended queue deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Update element */
|
||||
int? peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("Front element peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.PeekLast();
|
||||
Console.WriteLine("Rear element peekLast = " + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("After element 4 enqueues at rear, deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("After element 1 enqueues at front, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Element dequeue */
|
||||
int? popLast = deque.PopLast();
|
||||
Console.WriteLine("Rear dequeue element = " + popLast + ", after rear dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
int? popFirst = deque.PopFirst();
|
||||
Console.WriteLine("Front dequeue element = " + popFirst + ", after front dequeue, deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("Double-ended queue length size = " + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("Double-ended queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
106
en/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
106
en/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: linkedlist_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue {
|
||||
ListNode? front, rear; // Head node front, tail node rear
|
||||
int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
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 (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
} else if (rear != null) {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// Delete head node
|
||||
front = front?.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front!.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return [];
|
||||
|
||||
ListNode? node = front;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
LinkedListQueue queue = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
97
en/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
97
en/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* File: linkedlist_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
class LinkedListStack {
|
||||
ListNode? stackPeek; // Use head node as stack top
|
||||
int stkSize = 0; // Stack length
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
public int Size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
public void Push(int num) {
|
||||
ListNode node = new(num) {
|
||||
next = stackPeek
|
||||
};
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
stackPeek = stackPeek!.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek!.val;
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return [];
|
||||
|
||||
ListNode? node = stackPeek;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = res.Length - 1; i >= 0; i--) {
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
LinkedListStack stack = new();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
39
en/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
39
en/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access front of the queue element */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("Queue queue = " + string.Join(",", queue));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("Front element peek = " + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
int pop = queue.Dequeue();
|
||||
Console.WriteLine("Dequeue element pop = " + pop + ", after dequeue, queue = " + string.Join(",", queue));
|
||||
|
||||
/* Get the length of the queue */
|
||||
int size = queue.Count;
|
||||
Console.WriteLine("Queue length size = " + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
bool isEmpty = queue.Count == 0;
|
||||
Console.WriteLine("Queue is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
40
en/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
40
en/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Access top of the stack element */
|
||||
Stack<int> stack = new();
|
||||
|
||||
/* Elements push onto 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
|
||||
Console.WriteLine("Stack stack = " + string.Join(",", stack));
|
||||
|
||||
/* Return list for printing */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("Stack top element peek = " + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("Pop element pop = " + pop + ", after pop, stack = " + string.Join(",", stack));
|
||||
|
||||
/* Get the length of the stack */
|
||||
int size = stack.Count;
|
||||
Console.WriteLine("Stack length size = " + size);
|
||||
|
||||
/* Check if empty */
|
||||
bool isEmpty = stack.Count == 0;
|
||||
Console.WriteLine("Stack is empty = " + isEmpty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user