mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Reformat the C# codes.
Disable creating new line before open brace.
This commit is contained in:
@ -6,43 +6,36 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
{
|
||||
namespace hello_algo.chapter_stack_and_queue {
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
public class ArrayDeque
|
||||
{
|
||||
public class ArrayDeque {
|
||||
private readonly int[] nums; // 用于存储双向队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 双向队列长度
|
||||
|
||||
/* 构造方法 */
|
||||
public ArrayDeque(int capacity)
|
||||
{
|
||||
public ArrayDeque(int capacity) {
|
||||
this.nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 获取双向队列的容量 */
|
||||
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 越过数组头部后,回到尾部
|
||||
@ -50,10 +43,8 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
public void pushFirst(int num)
|
||||
{
|
||||
if (queSize == capacity())
|
||||
{
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
Console.WriteLine("双向队列已满");
|
||||
return;
|
||||
}
|
||||
@ -66,10 +57,8 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
public void pushLast(int num)
|
||||
{
|
||||
if (queSize == capacity())
|
||||
{
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
Console.WriteLine("双向队列已满");
|
||||
return;
|
||||
}
|
||||
@ -81,8 +70,7 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public int popFirst()
|
||||
{
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
// 队首指针向后移动一位
|
||||
front = index(front + 1);
|
||||
@ -91,53 +79,44 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public int popLast()
|
||||
{
|
||||
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);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
public int[] toArray()
|
||||
{
|
||||
public int[] toArray() {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++)
|
||||
{
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_deque
|
||||
{
|
||||
public class array_deque {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
ArrayDeque deque = new ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
|
||||
@ -9,41 +9,34 @@ using NUnit.Framework;
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue
|
||||
{
|
||||
class ArrayQueue {
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
|
||||
public ArrayQueue(int capacity)
|
||||
{
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
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;
|
||||
}
|
||||
@ -56,8 +49,7 @@ class ArrayQueue
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int pop()
|
||||
{
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
@ -66,31 +58,26 @@ class ArrayQueue
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
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++)
|
||||
{
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % this.capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue
|
||||
{
|
||||
public class array_queue {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
@ -120,8 +107,7 @@ public class array_queue
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
|
||||
@ -9,36 +9,30 @@ using NUnit.Framework;
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack
|
||||
{
|
||||
class ArrayStack {
|
||||
private List<int> stack;
|
||||
public ArrayStack()
|
||||
{
|
||||
public ArrayStack() {
|
||||
// 初始化列表(动态数组)
|
||||
stack = new();
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
public int size() {
|
||||
return stack.Count();
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
public void push(int num) {
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
public int pop() {
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
var val = peek();
|
||||
@ -47,25 +41,21 @@ class ArrayStack
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return stack[size() - 1];
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray()
|
||||
{
|
||||
public int[] toArray() {
|
||||
return stack.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack
|
||||
{
|
||||
public class array_stack {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化栈 */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
|
||||
@ -8,11 +8,9 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class deque
|
||||
{
|
||||
public class deque {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
||||
LinkedList<int> deque = new LinkedList<int>();
|
||||
|
||||
@ -6,17 +6,14 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
{
|
||||
namespace hello_algo.chapter_stack_and_queue {
|
||||
/* 双向链表节点 */
|
||||
public class ListNode
|
||||
{
|
||||
public class ListNode {
|
||||
public int val; // 节点值
|
||||
public ListNode? next; // 后继节点引用(指针)
|
||||
public ListNode? prev; // 前驱节点引用(指针)
|
||||
|
||||
public ListNode(int val)
|
||||
{
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
prev = null;
|
||||
next = null;
|
||||
@ -24,50 +21,42 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
public class LinkedListDeque
|
||||
{
|
||||
public class LinkedListDeque {
|
||||
private ListNode? front, rear; // 头节点 front, 尾节点 rear
|
||||
private int queSize = 0; // 双向队列的长度
|
||||
|
||||
public LinkedListDeque()
|
||||
{
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入队操作 */
|
||||
private void push(int num, bool isFront)
|
||||
{
|
||||
private void push(int num, bool isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (isEmpty())
|
||||
{
|
||||
if (isEmpty()) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
// 队首入队操作
|
||||
else if (isFront)
|
||||
{
|
||||
else if (isFront) {
|
||||
// 将 node 添加至链表头部
|
||||
front.prev = node;
|
||||
node.next = front;
|
||||
front = node; // 更新头节点
|
||||
}
|
||||
// 队尾入队操作
|
||||
else
|
||||
{
|
||||
else {
|
||||
// 将 node 添加至链表尾部
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
@ -78,35 +67,29 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队首入队 */
|
||||
public void pushFirst(int num)
|
||||
{
|
||||
public void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* 队尾入队 */
|
||||
public void pushLast(int num)
|
||||
{
|
||||
public void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
private int? pop(bool isFront)
|
||||
{
|
||||
private int? pop(bool isFront) {
|
||||
// 若队列为空,直接返回 null
|
||||
if (isEmpty())
|
||||
{
|
||||
if (isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int val;
|
||||
// 队首出队操作
|
||||
if (isFront)
|
||||
{
|
||||
if (isFront) {
|
||||
val = front.val; // 暂存头节点值
|
||||
// 删除头节点
|
||||
ListNode fNext = front.next;
|
||||
if (fNext != null)
|
||||
{
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
}
|
||||
@ -114,13 +97,11 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
front = fNext; // 更新头节点
|
||||
}
|
||||
// 队尾出队操作
|
||||
else
|
||||
{
|
||||
else {
|
||||
val = rear.val; // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
ListNode rPrev = rear.prev;
|
||||
if (rPrev != null)
|
||||
{
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
}
|
||||
@ -133,36 +114,30 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
public int? popFirst()
|
||||
{
|
||||
public int? popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* 队尾出队 */
|
||||
public int? popLast()
|
||||
{
|
||||
public int? popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int? peekFirst()
|
||||
{
|
||||
public int? peekFirst() {
|
||||
return isEmpty() ? null : front.val;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
public int? peekLast()
|
||||
{
|
||||
public int? peekLast() {
|
||||
return isEmpty() ? null : rear.val;
|
||||
}
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
public int[] toArray()
|
||||
{
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
@ -171,11 +146,9 @@ namespace hello_algo.chapter_stack_and_queue
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_deque
|
||||
{
|
||||
public class linkedlist_deque {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化双向队列 */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
|
||||
@ -10,43 +10,35 @@ using NUnit.Framework;
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue
|
||||
{
|
||||
class LinkedListQueue {
|
||||
private ListNode? front, rear; // 头节点 front ,尾节点 rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue()
|
||||
{
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num)
|
||||
{
|
||||
public void push(int num) {
|
||||
// 尾节点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾节点都指向该节点
|
||||
if (front == null)
|
||||
{
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// 如果队列不为空,则将该节点添加到尾节点后
|
||||
}
|
||||
else if (rear != null)
|
||||
{
|
||||
} else if (rear != null) {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
@ -54,8 +46,7 @@ class LinkedListQueue
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int pop()
|
||||
{
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// 删除头节点
|
||||
front = front?.next;
|
||||
@ -64,23 +55,20 @@ class LinkedListQueue
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
public int peek() {
|
||||
if (size() == 0 || front == null)
|
||||
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()];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
@ -88,11 +76,9 @@ class LinkedListQueue
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_queue
|
||||
{
|
||||
public class linkedlist_queue {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化队列 */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
|
||||
@ -10,31 +10,26 @@ using NUnit.Framework;
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack
|
||||
{
|
||||
class LinkedListStack {
|
||||
private ListNode? stackPeek; // 将头节点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
public LinkedListStack()
|
||||
{
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
public int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
public bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
public void push(int num) {
|
||||
ListNode node = new ListNode(num);
|
||||
node.next = stackPeek;
|
||||
stackPeek = node;
|
||||
@ -42,8 +37,7 @@ class LinkedListStack
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
public int pop() {
|
||||
if (stackPeek == null)
|
||||
throw new Exception();
|
||||
|
||||
@ -54,23 +48,20 @@ class LinkedListStack
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
public int peek() {
|
||||
if (size() == 0 || stackPeek == null)
|
||||
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()];
|
||||
for (int i = res.Length - 1; i >= 0; i--)
|
||||
{
|
||||
for (int i = res.Length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
@ -78,11 +69,9 @@ class LinkedListStack
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_stack
|
||||
{
|
||||
public class linkedlist_stack {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化栈 */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
|
||||
@ -8,11 +8,9 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class queue
|
||||
{
|
||||
public class queue {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化队列 */
|
||||
Queue<int> queue = new();
|
||||
|
||||
|
||||
@ -8,11 +8,9 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class stack
|
||||
{
|
||||
public class stack {
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
public void Test() {
|
||||
/* 初始化栈 */
|
||||
Stack<int> stack = new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user