mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +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);
|
||||
|
||||
Reference in New Issue
Block a user