Files
hello-algo/en/codes/csharp/chapter_stack_and_queue/array_queue.cs
T
Yudong Jin 2778a6f9c7 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
2025-12-31 07:44:52 +08:00

115 lines
3.1 KiB
C#

/**
* 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()));
}
}
}