mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 18:31:08 +08:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -6,127 +6,125 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue
|
||||
{
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
|
||||
/* 基于环形数组实现的队列 */
|
||||
class ArrayQueue
|
||||
public ArrayQueue(int capacity)
|
||||
{
|
||||
private int[] nums; // 用于存储队列元素的数组
|
||||
private int front; // 队首指针,指向队首元素
|
||||
private int queSize; // 队列长度
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
public ArrayQueue(int capacity)
|
||||
{
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
/* 获取队列的容量 */
|
||||
public int capacity()
|
||||
{
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 获取队列的容量 */
|
||||
public int capacity()
|
||||
{
|
||||
return nums.Length;
|
||||
}
|
||||
/* 获取队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
return queSize;
|
||||
}
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty()
|
||||
/* 入队 */
|
||||
public void push(int num)
|
||||
{
|
||||
if (queSize == capacity())
|
||||
{
|
||||
return queSize == 0;
|
||||
Console.WriteLine("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
int rear = (front + queSize) % capacity();
|
||||
// 尾结点后添加 num
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num)
|
||||
{
|
||||
if (queSize == capacity())
|
||||
{
|
||||
Console.WriteLine("队列已满");
|
||||
return;
|
||||
}
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
||||
int rear = (front + queSize) % capacity();
|
||||
// 尾结点后添加 num
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
/* 出队 */
|
||||
public int poll()
|
||||
{
|
||||
int num = peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int poll()
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
public int[] toArray()
|
||||
{
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++)
|
||||
{
|
||||
int num = peek();
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
res[i] = nums[j % this.capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return nums[front];
|
||||
}
|
||||
int peek = queue.peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 返回数组 */
|
||||
public int[] toArray()
|
||||
/* 元素出队 */
|
||||
int poll = queue.poll();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++)
|
||||
{
|
||||
res[i] = nums[j % this.capacity()];
|
||||
}
|
||||
return res;
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
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();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue.poll();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,93 +6,91 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack
|
||||
{
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack
|
||||
private List<int> stack;
|
||||
public ArrayStack()
|
||||
{
|
||||
private List<int> stack;
|
||||
public ArrayStack()
|
||||
{
|
||||
// 初始化列表(动态数组)
|
||||
stack = new();
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
return stack.Count();
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
var val = peek();
|
||||
stack.RemoveAt(size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return stack[size() - 1];
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray()
|
||||
{
|
||||
return stack.ToArray();
|
||||
}
|
||||
// 初始化列表(动态数组)
|
||||
stack = new();
|
||||
}
|
||||
|
||||
public class array_stack
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化栈 */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
return stack.Count();
|
||||
}
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray()));
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int peek = stack.peek();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray()));
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
var val = peek();
|
||||
stack.RemoveAt(size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (isEmpty())
|
||||
throw new Exception();
|
||||
return stack[size() - 1];
|
||||
}
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray()
|
||||
{
|
||||
return stack.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化栈 */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
/* 元素入栈 */
|
||||
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();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray()));
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,44 +6,43 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class deque
|
||||
{
|
||||
public class deque
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化双向队列 */
|
||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
||||
LinkedList<int> deque = new LinkedList<int>();
|
||||
/* 初始化双向队列 */
|
||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
||||
LinkedList<int> deque = new LinkedList<int>();
|
||||
|
||||
/* 元素入队 */
|
||||
deque.AddLast(2); // 添加至队尾
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // 添加至队首
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
|
||||
/* 元素入队 */
|
||||
deque.AddLast(2); // 添加至队尾
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // 添加至队首
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.First.Value; // 队首元素
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.Last.Value; // 队尾元素
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.First.Value; // 队首元素
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.Last.Value; // 队尾元素
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素出队 */
|
||||
deque.RemoveFirst(); // 队首元素出队
|
||||
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
deque.RemoveLast(); // 队尾元素出队
|
||||
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
/* 元素出队 */
|
||||
deque.RemoveFirst(); // 队首元素出队
|
||||
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
deque.RemoveLast(); // 队尾元素出队
|
||||
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("双向队列长度 size = " + size);
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
||||
}
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,118 +7,117 @@
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue
|
||||
{
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue
|
||||
private ListNode? front, rear; // 头结点 front ,尾结点 rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue()
|
||||
{
|
||||
private ListNode? front, rear; // 头结点 front ,尾结点 rear
|
||||
private int queSize = 0;
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
public LinkedListQueue()
|
||||
{
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
/* 获取队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 获取队列的长度 */
|
||||
public int size()
|
||||
{
|
||||
return queSize;
|
||||
}
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
public bool isEmpty()
|
||||
/* 入队 */
|
||||
public void push(int num)
|
||||
{
|
||||
// 尾结点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
if (front == null)
|
||||
{
|
||||
return size() == 0;
|
||||
front = node;
|
||||
rear = node;
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
}
|
||||
else if (rear != null)
|
||||
{
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 入队 */
|
||||
public void push(int num)
|
||||
{
|
||||
// 尾结点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
if (front == null)
|
||||
{
|
||||
front = node;
|
||||
rear = node;
|
||||
// 如果队列不为空,则将该结点添加到尾结点后
|
||||
}
|
||||
else if (rear != null)
|
||||
{
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
/* 出队 */
|
||||
public int poll()
|
||||
{
|
||||
int num = peek();
|
||||
// 删除头结点
|
||||
front = front?.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
public int poll()
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (size() == 0 || front == null)
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
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++)
|
||||
{
|
||||
int num = peek();
|
||||
// 删除头结点
|
||||
front = front?.next;
|
||||
queSize--;
|
||||
return num;
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_queue
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + String.Join(",", queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (size() == 0 || front == null)
|
||||
throw new Exception();
|
||||
return front.val;
|
||||
}
|
||||
int peek = queue.peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
public int[] toArray()
|
||||
{
|
||||
if (front == null)
|
||||
return Array.Empty<int>();
|
||||
/* 元素出队 */
|
||||
int poll = queue.poll();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.toArray()));
|
||||
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
for (int i = 0; i < res.Length; i++)
|
||||
{
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
|
||||
public class linkedlist_queue
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
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();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue.poll();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.toArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.isEmpty();
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,107 +7,107 @@
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack
|
||||
{
|
||||
class LinkedListStack
|
||||
private ListNode? stackPeek; // 将头结点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
|
||||
public LinkedListStack()
|
||||
{
|
||||
private ListNode? stackPeek; // 将头结点作为栈顶
|
||||
private int stkSize = 0; // 栈的长度
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
public LinkedListStack()
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
ListNode node = new ListNode(num);
|
||||
node.next = stackPeek;
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
if (stackPeek == null)
|
||||
throw new Exception();
|
||||
|
||||
int num = peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (size() == 0 || stackPeek == null)
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
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--)
|
||||
{
|
||||
stackPeek = null;
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
public int size()
|
||||
{
|
||||
return stkSize;
|
||||
}
|
||||
public class linkedlist_stack
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化栈 */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
public bool isEmpty()
|
||||
{
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
public void push(int num)
|
||||
{
|
||||
ListNode node = new ListNode(num);
|
||||
node.next = stackPeek;
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
public int pop()
|
||||
{
|
||||
if (stackPeek == null)
|
||||
throw new Exception();
|
||||
|
||||
int num = peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
/* 元素入栈 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
Console.WriteLine("栈 stack = " + String.Join(",", stack.toArray()));
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
public int peek()
|
||||
{
|
||||
if (size() == 0 || stackPeek==null)
|
||||
throw new Exception();
|
||||
return stackPeek.val;
|
||||
}
|
||||
int peek = stack.peek();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public int[] toArray()
|
||||
{
|
||||
if (stackPeek == null)
|
||||
return Array.Empty<int>();
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",", stack.toArray()));
|
||||
|
||||
ListNode node = stackPeek;
|
||||
int[] res = new int[size()];
|
||||
for (int i = res.Length - 1; i >= 0; i--)
|
||||
{
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
|
||||
public class linkedlist_stack
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化栈 */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
/* 元素入栈 */
|
||||
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();
|
||||
Console.WriteLine("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + String.Join(",",stack.toArray()));
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.isEmpty();
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,40 +6,38 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class queue
|
||||
{
|
||||
public class queue
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
Queue<int> queue = new();
|
||||
/* 初始化队列 */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray()));
|
||||
/* 元素入队 */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue.Dequeue();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray()));
|
||||
/* 元素出队 */
|
||||
int poll = queue.Dequeue();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.Count();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.Count();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.Count() == 0;
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.Count() == 0;
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,40 +6,39 @@
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class stack
|
||||
{
|
||||
public class stack
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化栈 */
|
||||
Stack<int> stack = new();
|
||||
/* 初始化栈 */
|
||||
Stack<int> stack = new();
|
||||
|
||||
/* 元素入栈 */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
// 请注意,stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
|
||||
/* 元素入栈 */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
// 请注意,stack.ToArray() 得到的是倒序序列,即索引 0 为栈顶
|
||||
Console.WriteLine("栈 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("栈顶元素 peek = " + 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.Count();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.Count();
|
||||
Console.WriteLine("栈的长度 size = " + size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.Count() == 0;
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
/* 判断是否为空 */
|
||||
bool isEmpty = stack.Count() == 0;
|
||||
Console.WriteLine("栈是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user