mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,18 +8,18 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
public class ArrayDeque {
|
||||
private readonly int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 双向队列长度
|
||||
int[] nums; // 用于存储双向队列元素的数组
|
||||
int front; // 队首指针,指向队首元素
|
||||
int queSize; // 双向队列长度
|
||||
|
||||
/* 构造方法 */
|
||||
public ArrayDeque(int capacity) {
|
||||
this.nums = new int[capacity];
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 获取双向队列的容量 */
|
||||
public int Capacity() {
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ public class ArrayDeque {
|
||||
}
|
||||
|
||||
/* 计算环形数组索引 */
|
||||
private int Index(int i) {
|
||||
int Index(int i) {
|
||||
// 通过取余操作实现数组首尾相连
|
||||
// 当 i 越过数组尾部后,回到头部
|
||||
// 当 i 越过数组头部后,回到尾部
|
||||
|
||||
@@ -8,9 +8,9 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private readonly int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
int[] nums; // 用于存储队列元素的数组
|
||||
int front; // 队首指针,指向队首元素
|
||||
int queSize; // 队列长度
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
@@ -18,7 +18,7 @@ class ArrayQueue {
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
public int Capacity() {
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
private readonly List<int> stack;
|
||||
List<int> stack;
|
||||
public ArrayStack() {
|
||||
// 初始化列表(动态数组)
|
||||
stack = new();
|
||||
stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
@@ -47,7 +47,7 @@ class ArrayStack {
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] ToArray() {
|
||||
return stack.ToArray();
|
||||
return [.. stack];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,22 +7,16 @@
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 双向链表节点 */
|
||||
public class ListNode {
|
||||
public int val; // 节点值
|
||||
public ListNode? next; // 后继节点引用
|
||||
public ListNode? prev; // 前驱节点引用
|
||||
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
prev = null;
|
||||
next = null;
|
||||
}
|
||||
public class ListNode(int val) {
|
||||
public int val = val; // 节点值
|
||||
public ListNode? next = null; // 后继节点引用
|
||||
public ListNode? prev = null; // 前驱节点引用
|
||||
}
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
public class LinkedListDeque {
|
||||
private ListNode? front, rear; // 头节点 front, 尾节点 rear
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
ListNode? front, rear; // 头节点 front, 尾节点 rear
|
||||
int queSize = 0; // 双向队列的长度
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
@@ -40,7 +34,7 @@ public class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 入队操作 */
|
||||
private void Push(int num, bool isFront) {
|
||||
void Push(int num, bool isFront) {
|
||||
ListNode node = new(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (IsEmpty()) {
|
||||
@@ -50,14 +44,14 @@ public class LinkedListDeque {
|
||||
// 队首入队操作
|
||||
else if (isFront) {
|
||||
// 将 node 添加至链表头部
|
||||
front.prev = node;
|
||||
front!.prev = node;
|
||||
node.next = front;
|
||||
front = node; // 更新头节点
|
||||
}
|
||||
// 队尾入队操作
|
||||
else {
|
||||
// 将 node 添加至链表尾部
|
||||
rear.next = node;
|
||||
rear!.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // 更新尾节点
|
||||
}
|
||||
@@ -76,7 +70,7 @@ public class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
private int? Pop(bool isFront) {
|
||||
int? Pop(bool isFront) {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
int? val;
|
||||
@@ -87,7 +81,7 @@ public class LinkedListDeque {
|
||||
ListNode? fNext = front?.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
front!.next = null;
|
||||
}
|
||||
front = fNext; // 更新头节点
|
||||
}
|
||||
@@ -98,7 +92,7 @@ public class LinkedListDeque {
|
||||
ListNode? rPrev = rear?.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
rear!.prev = null;
|
||||
}
|
||||
rear = rPrev; // 更新尾节点
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
private ListNode? front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0;
|
||||
ListNode? front, rear; // 头节点 front ,尾节点 rear
|
||||
int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
@@ -55,18 +55,18 @@ class LinkedListQueue {
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
return front!.val;
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return Array.Empty<int>();
|
||||
return [];
|
||||
|
||||
ListNode node = front;
|
||||
ListNode? node = front;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node.val;
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
private ListNode? stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
ListNode? stackPeek; // 将头节点作为栈顶
|
||||
int stkSize = 0; // 栈的长度
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
@@ -37,7 +37,7 @@ class LinkedListStack {
|
||||
/* 出栈 */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stackPeek = stackPeek!.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
@@ -46,18 +46,18 @@ class LinkedListStack {
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
return stackPeek!.val;
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return Array.Empty<int>();
|
||||
return [];
|
||||
|
||||
ListNode node = stackPeek;
|
||||
ListNode? node = stackPeek;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = res.Length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user