This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions

View File

@ -777,22 +777,16 @@ comments: true
```csharp title="linkedlist_deque.cs"
/* 双向链表节点 */
class ListNode {
public int val; // 节点值
public ListNode? next; // 后继节点引用
public ListNode? prev; // 前驱节点引用
public ListNode(int val) {
this.val = val;
prev = null;
next = null;
}
class ListNode(int val) {
public int val = val; // 节点值
public ListNode? next = null; // 后继节点引用
public ListNode? prev = null; // 前驱节点引用
}
/* 基于双向链表实现的双向队列 */
class LinkedListDeque {
private ListNode? front, rear; // 头节点 front, 尾节点 rear
private int queSize = 0; // 双向队列的长度
ListNode? front, rear; // 头节点 front, 尾节点 rear
int queSize = 0; // 双向队列的长度
public LinkedListDeque() {
front = null;
@ -810,7 +804,7 @@ comments: true
}
/* 入队操作 */
private void Push(int num, bool isFront) {
void Push(int num, bool isFront) {
ListNode node = new(num);
// 若链表为空,则令 front, rear 都指向 node
if (IsEmpty()) {
@ -820,14 +814,14 @@ comments: true
// 队首入队操作
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; // 更新尾节点
}
@ -846,7 +840,7 @@ comments: true
}
/* 出队操作 */
private int? Pop(bool isFront) {
int? Pop(bool isFront) {
if (IsEmpty())
throw new Exception();
int? val;
@ -857,7 +851,7 @@ comments: true
ListNode? fNext = front?.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
front!.next = null;
}
front = fNext; // 更新头节点
}
@ -868,7 +862,7 @@ comments: true
ListNode? rPrev = rear?.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
rear!.prev = null;
}
rear = rPrev; // 更新尾节点
}
@ -2343,18 +2337,18 @@ comments: true
```csharp title="array_deque.cs"
/* 基于环形数组实现的双向队列 */
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;
}
@ -2369,7 +2363,7 @@ comments: true
}
/* 计算环形数组索引 */
private int Index(int i) {
int Index(int i) {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部

View File

@ -547,8 +547,8 @@ comments: true
```csharp title="linkedlist_queue.cs"
/* 基于链表实现的队列 */
class LinkedListQueue {
private ListNode? front, rear; // 头节点 front ,尾节点 rear
private int queSize = 0;
ListNode? front, rear; // 头节点 front ,尾节点 rear
int queSize = 0;
public LinkedListQueue() {
front = null;
@ -594,18 +594,18 @@ comments: true
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;
@ -1445,9 +1445,9 @@ comments: true
```csharp title="array_queue.cs"
/* 基于环形数组实现的队列 */
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];
@ -1455,7 +1455,7 @@ comments: true
}
/* 获取队列的容量 */
public int Capacity() {
int Capacity() {
return nums.Length;
}

View File

@ -518,8 +518,8 @@ comments: true
```csharp title="linkedlist_stack.cs"
/* 基于链表实现的栈 */
class LinkedListStack {
private ListNode? stackPeek; // 将头节点作为栈顶
private int stkSize = 0; // 栈的长度
ListNode? stackPeek; // 将头节点作为栈顶
int stkSize = 0; // 栈的长度
public LinkedListStack() {
stackPeek = null;
@ -547,7 +547,7 @@ comments: true
/* 出栈 */
public int Pop() {
int num = Peek();
stackPeek = stackPeek.next;
stackPeek = stackPeek!.next;
stkSize--;
return num;
}
@ -556,18 +556,18 @@ comments: true
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;
@ -1237,10 +1237,10 @@ comments: true
```csharp title="array_stack.cs"
/* 基于数组实现的栈 */
class ArrayStack {
private readonly List<int> stack;
List<int> stack;
public ArrayStack() {
// 初始化列表(动态数组)
stack = new();
stack = [];
}
/* 获取栈的长度 */
@ -1276,7 +1276,7 @@ comments: true
/* 将 List 转化为 Array 并返回 */
public int[] ToArray() {
return stack.ToArray();
return [.. stack];
}
}
```