mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-01 17:53:18 +08:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@@ -19,95 +19,95 @@ public class ArrayDeque {
|
||||
}
|
||||
|
||||
/* 获取双向队列的容量 */
|
||||
public int capacity() {
|
||||
public int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
public bool isEmpty() {
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 计算环形数组索引 */
|
||||
private int index(int i) {
|
||||
private int Index(int i) {
|
||||
// 通过取余操作实现数组首尾相连
|
||||
// 当 i 越过数组尾部后,回到头部
|
||||
// 当 i 越过数组头部后,回到尾部
|
||||
return (i + capacity()) % capacity();
|
||||
return (i + Capacity()) % Capacity();
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
public void PushFirst(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("双向队列已满");
|
||||
return;
|
||||
}
|
||||
// 队首指针向左移动一位
|
||||
// 通过取余操作,实现 front 越过数组头部后回到尾部
|
||||
front = index(front - 1);
|
||||
front = Index(front - 1);
|
||||
// 将 num 添加至队首
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
public void PushLast(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("双向队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
int rear = index(front + queSize);
|
||||
int rear = Index(front + queSize);
|
||||
// 将 num 添加至队尾
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
public int PopFirst() {
|
||||
int num = PeekFirst();
|
||||
// 队首指针向后移动一位
|
||||
front = index(front + 1);
|
||||
front = Index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public int popLast() {
|
||||
int num = peekLast();
|
||||
public int PopLast() {
|
||||
int num = PeekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peekFirst() {
|
||||
if (isEmpty()) {
|
||||
public int PeekFirst() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int peekLast() {
|
||||
if (isEmpty()) {
|
||||
public int PeekLast() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
// 计算尾元素索引
|
||||
int last = index(front + queSize - 1);
|
||||
int last = Index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
public int[] toArray() {
|
||||
public int[] ToArray() {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
res[i] = nums[Index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -117,36 +117,36 @@ public class array_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
ArrayDeque deque = new ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
|
||||
ArrayDeque deque = new(10);
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.peekFirst();
|
||||
int peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
int peekLast = deque.PeekLast();
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素入队 */
|
||||
deque.pushLast(4);
|
||||
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 元素出队 */
|
||||
int popLast = deque.popLast();
|
||||
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
int popLast = deque.PopLast();
|
||||
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int popFirst = deque.PopFirst();
|
||||
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.size();
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.isEmpty();
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private readonly int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
|
||||
@@ -18,56 +18,56 @@ class ArrayQueue {
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
public int capacity() {
|
||||
public int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty() {
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num) {
|
||||
if (queSize == capacity()) {
|
||||
public void Push(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
int rear = (front + queSize) % capacity();
|
||||
int rear = (front + queSize) % Capacity();
|
||||
// 将 num 添加至队尾
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
front = (front + 1) % Capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
public int[] toArray() {
|
||||
public int[] ToArray() {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % this.capacity()];
|
||||
res[i] = nums[j % this.Capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -78,37 +78,37 @@ public class array_queue {
|
||||
public void Test() {
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
ArrayQueue queue = new(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.peek();
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int pop = queue.pop();
|
||||
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
queue.Push(i);
|
||||
queue.Pop();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,45 +8,45 @@ namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
private List<int> stack;
|
||||
private readonly List<int> stack;
|
||||
public ArrayStack() {
|
||||
// 初始化列表(动态数组)
|
||||
stack = new();
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size() {
|
||||
return stack.Count();
|
||||
public int Size() {
|
||||
return stack.Count;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num) {
|
||||
public void Push(int num) {
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop() {
|
||||
if (isEmpty())
|
||||
public int Pop() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
var val = peek();
|
||||
stack.RemoveAt(size() - 1);
|
||||
var val = Peek();
|
||||
stack.RemoveAt(Size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stack[size() - 1];
|
||||
return stack[Size() - 1];
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray() {
|
||||
public int[] ToArray() {
|
||||
return stack.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -55,30 +55,30 @@ public class array_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化栈 */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
ArrayStack stack = new();
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int peek = stack.peek();
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray()));
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class deque {
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
||||
LinkedList<int> deque = new LinkedList<int>();
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* 元素入队 */
|
||||
deque.AddLast(2); // 添加至队尾
|
||||
@@ -22,9 +22,9 @@ public class deque {
|
||||
Console.WriteLine("双向队列 deque = " + string.Join(",", deque));
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.First.Value; // 队首元素
|
||||
int? peekFirst = deque.First?.Value; // 队首元素
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.Last.Value; // 队尾元素
|
||||
int? peekLast = deque.Last?.Value; // 队尾元素
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素出队 */
|
||||
|
||||
@@ -30,20 +30,20 @@ public class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 入队操作 */
|
||||
private void push(int num, bool isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
private void Push(int num, bool isFront) {
|
||||
ListNode node = new(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (isEmpty()) {
|
||||
if (IsEmpty()) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
@@ -66,25 +66,25 @@ public class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
public void pushFirst(int num) {
|
||||
push(num, true);
|
||||
public void PushFirst(int num) {
|
||||
Push(num, true);
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
public void pushLast(int num) {
|
||||
push(num, false);
|
||||
public void PushLast(int num) {
|
||||
Push(num, false);
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
private int? pop(bool isFront) {
|
||||
if (isEmpty())
|
||||
private int? Pop(bool isFront) {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
int val;
|
||||
int? val;
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
val = front.val; // 暂存头节点值
|
||||
val = front?.val; // 暂存头节点值
|
||||
// 删除头节点
|
||||
ListNode fNext = front.next;
|
||||
ListNode? fNext = front?.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
@@ -93,9 +93,9 @@ public class LinkedListDeque {
|
||||
}
|
||||
// 队尾出队操作
|
||||
else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
val = rear?.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
ListNode? rPrev = rear?.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
@@ -108,36 +108,36 @@ public class LinkedListDeque {
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public int? popFirst() {
|
||||
return pop(true);
|
||||
public int? PopFirst() {
|
||||
return Pop(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public int? popLast() {
|
||||
return pop(false);
|
||||
public int? PopLast() {
|
||||
return Pop(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int? peekFirst() {
|
||||
if (isEmpty())
|
||||
public int? PeekFirst() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
return front?.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int? peekLast() {
|
||||
if (isEmpty())
|
||||
public int? PeekLast() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return rear.val;
|
||||
return rear?.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
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;
|
||||
res[i] = node?.val;
|
||||
node = node?.next;
|
||||
}
|
||||
|
||||
return res;
|
||||
@@ -148,36 +148,36 @@ public class linkedlist_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
|
||||
LinkedListDeque deque = new();
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int? peekFirst = deque.peekFirst();
|
||||
int? peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.peekLast();
|
||||
int? peekLast = deque.PeekLast();
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素入队 */
|
||||
deque.pushLast(4);
|
||||
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 元素出队 */
|
||||
int? popLast = deque.popLast();
|
||||
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
int? popFirst = deque.popFirst();
|
||||
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
|
||||
int? popLast = deque.PopLast();
|
||||
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int? popFirst = deque.PopFirst();
|
||||
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.size();
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.isEmpty();
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,19 +17,19 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num) {
|
||||
public void Push(int num) {
|
||||
// 尾节点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
ListNode node = new(num);
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
if (front == null) {
|
||||
front = node;
|
||||
@@ -43,8 +43,8 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// 删除头节点
|
||||
front = front?.next;
|
||||
queSize--;
|
||||
@@ -52,19 +52,19 @@ class LinkedListQueue {
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
public int[] toArray() {
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return Array.Empty<int>();
|
||||
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
int[] res = new int[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
@@ -77,30 +77,30 @@ public class linkedlist_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化队列 */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
LinkedListQueue queue = new();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.peek();
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int pop = queue.pop();
|
||||
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,45 +16,46 @@ class LinkedListStack {
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num) {
|
||||
ListNode node = new ListNode(num);
|
||||
node.next = stackPeek;
|
||||
public void Push(int num) {
|
||||
ListNode node = new(num) {
|
||||
next = stackPeek
|
||||
};
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray() {
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return Array.Empty<int>();
|
||||
|
||||
ListNode node = stackPeek;
|
||||
int[] res = new int[size()];
|
||||
int[] res = new int[Size()];
|
||||
for (int i = res.Length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
@@ -67,30 +68,30 @@ public class linkedlist_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 初始化栈 */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
LinkedListStack stack = new();
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.toArray()));
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int peek = stack.peek();
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.toArray()));
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user