Merge pull request #159 from haptear/master

完善所有c#相关的文档和代码
This commit is contained in:
Yudong Jin
2022-12-24 16:20:05 +08:00
committed by GitHub
51 changed files with 4414 additions and 80 deletions

View File

@@ -205,7 +205,27 @@ comments: true
=== "C#"
```csharp title="queue.cs"
/* 初始化队列 */
Queue<int> queue = new();
/* 元素入队 */
queue.Enqueue(1);
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(5);
queue.Enqueue(4);
/* 访问队首元素 */
int peek = queue.Peek();
/* 元素出队 */
int poll = queue.Dequeue();
/* 获取队列的长度 */
int size = queue.Count();
/* 判断队列是否为空 */
bool isEmpty = queue.Count() == 0;
```
## 队列实现
@@ -532,7 +552,68 @@ comments: true
=== "C#"
```csharp title="linkedlist_queue.cs"
/* 基于链表实现的队列 */
class LinkedListQueue
{
private ListNode? front, rear; // 头结点 front ,尾结点 rear
private int queSize = 0;
public LinkedListQueue()
{
front = null;
rear = null;
}
/* 获取队列的长度 */
public int size()
{
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入队 */
public void offer(int num)
{
// 尾结点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
if (front == null)
{
front = node;
rear = node;
// 如果队列不为空,则将该结点添加到尾结点后
}
else if (rear != null)
{
rear.next = node;
rear = node;
}
queSize++;
}
/* 出队 */
public int poll()
{
int num = peek();
// 删除头结点
front = front?.next;
queSize--;
return num;
}
/* 访问队首元素 */
public int peek()
{
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
}
```
### 基于数组的实现
@@ -880,7 +961,61 @@ comments: true
=== "C#"
```csharp title="array_queue.cs"
/* 基于环形数组实现的队列 */
class ArrayQueue {
private int[] nums; // 用于存储队列元素的数组
private int front = 0; // 头指针,指向队首
private int rear = 0; // 尾指针,指向队尾 + 1
public ArrayQueue(int capacity) {
// 初始化数组
nums = new int[capacity];
}
/* 获取队列的容量 */
public int capacity() {
return nums.Length;
}
/* 获取队列的长度 */
public int size() {
int capacity = this.capacity();
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
return (capacity + rear - front) % capacity;
}
/* 判断队列是否为空 */
public bool isEmpty() {
return rear - front == 0;
}
/* 入队 */
public void offer(int num) {
if (size() == capacity()) {
Console.WriteLine("队列已满");
return;
}
// 尾结点后添加 num
nums[rear] = num;
// 尾指针向后移动一位,越过尾部后返回到数组头部
rear = (rear + 1) % capacity();
}
/* 出队 */
public int poll() {
int num = peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
return num;
}
/* 访问队首元素 */
public int peek() {
if (isEmpty())
throw new Exception();
return nums[front];
}
}
```
## 队列典型应用

View File

@@ -203,7 +203,27 @@ comments: true
=== "C#"
```csharp title="stack.cs"
/* 初始化栈 */
Stack<int> stack = new ();
/* 元素入栈 */
stack.Push(1);
stack.Push(3);
stack.Push(2);
stack.Push(5);
stack.Push(4);
/* 访问栈顶元素 */
int peek = stack.Peek();
/* 元素出栈 */
int pop = stack.Pop();
/* 获取栈的长度 */
int size = stack.Count();
/* 判断是否为空 */
bool isEmpty = stack.Count()==0;
```
## 栈的实现
@@ -520,7 +540,54 @@ comments: true
=== "C#"
```csharp title="linkedlist_stack.cs"
/* 基于链表实现的栈 */
class LinkedListStack
{
private ListNode stackPeek; // 将头结点作为栈顶
private int stkSize = 0; // 栈的长度
public LinkedListStack()
{
stackPeek = null;
}
/* 获取栈的长度 */
public int size()
{
return stkSize;
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
ListNode node = new ListNode(num);
node.next = stackPeek;
stackPeek = node;
stkSize++;
}
/* 出栈 */
public int pop()
{
int num = peek();
stackPeek = stackPeek?.next;
stkSize--;
return num;
}
/* 访问栈顶元素 */
public int peek()
{
if (size() == 0)
throw new Exception();
return stackPeek.val;
}
}
```
### 基于数组的实现
@@ -760,7 +827,52 @@ comments: true
=== "C#"
```csharp title="array_stack.cs"
/* 基于数组实现的栈 */
class ArrayStack
{
private List<int> stack;
public ArrayStack()
{
// 初始化列表(动态数组)
stack = new();
}
/* 获取栈的长度 */
public int size()
{
return stack.Count();
}
/* 判断栈是否为空 */
public bool isEmpty()
{
return size() == 0;
}
/* 入栈 */
public void push(int num)
{
stack.Add(num);
}
/* 出栈 */
public int pop()
{
if (isEmpty())
throw new Exception();
var val = peek();
stack.RemoveAt(size() - 1);
return val;
}
/* 访问栈顶元素 */
public int peek()
{
if (isEmpty())
throw new Exception();
return stack[size() - 1];
}
}
```
!!! tip