refactor: use global using to reduce usings in each file (#486)

This commit is contained in:
hpstory 2023-05-06 22:48:02 +08:00 committed by GitHub
parent db6caf0d43
commit 40319e7f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
48 changed files with 311 additions and 428 deletions

View File

@ -0,0 +1,2 @@
global using NUnit.Framework;
global using hello_algo.utils;

View File

@ -2,8 +2,6 @@
// Created Time: 2022-12-14 // Created Time: 2022-12-14
// Author: mingXta (1195669834@qq.com) // Author: mingXta (1195669834@qq.com)
using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist; namespace hello_algo.chapter_array_and_linkedlist;
public class array { public class array {

View File

@ -2,9 +2,6 @@
// Created Time: 2022-12-16 // Created Time: 2022-12-16
// Author: mingXta (1195669834@qq.com) // Author: mingXta (1195669834@qq.com)
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist; namespace hello_algo.chapter_array_and_linkedlist;
public class linked_list { public class linked_list {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist; namespace hello_algo.chapter_array_and_linkedlist;
public class list { public class list {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_array_and_linkedlist; namespace hello_algo.chapter_array_and_linkedlist;
/* 列表类简易实现 */ /* 列表类简易实现 */

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class n_queens { public class n_queens {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class permutations_i { public class permutations_i {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class permutations_ii { public class permutations_ii {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_i_compact { public class preorder_traversal_i_compact {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_ii_compact { public class preorder_traversal_ii_compact {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_compact { public class preorder_traversal_iii_compact {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_backtracking; namespace hello_algo.chapter_backtracking;
public class preorder_traversal_iii_template { public class preorder_traversal_iii_template {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_binary_search; namespace hello_algo.chapter_binary_search;
public class binary_search { public class binary_search {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity; namespace hello_algo.chapter_computational_complexity;
public class space_complexity { public class space_complexity {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity; namespace hello_algo.chapter_computational_complexity;
public class time_complexity { public class time_complexity {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_computational_complexity; namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity { public class worst_best_time_complexity {

View File

@ -4,9 +4,6 @@
* Author: zjkung1123 (zjkung1123@gmail.com) * Author: zjkung1123 (zjkung1123@gmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_graph; namespace hello_algo.chapter_graph;
/* 基于邻接表实现的无向图类 */ /* 基于邻接表实现的无向图类 */

View File

@ -4,9 +4,6 @@
* Author: zjkung1123 (zjkung1123@gmail.com) * Author: zjkung1123 (zjkung1123@gmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_graph; namespace hello_algo.chapter_graph;
/* 基于邻接矩阵实现的无向图类 */ /* 基于邻接矩阵实现的无向图类 */

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_graph; namespace hello_algo.chapter_graph;
public class graph_bfs { public class graph_bfs {

View File

@ -4,9 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_graph; namespace hello_algo.chapter_graph;
public class graph_dfs { public class graph_dfs {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_hashing; namespace hello_algo.chapter_hashing;
/* 键值对 int->string */ /* 键值对 int->string */

View File

@ -5,9 +5,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_hashing; namespace hello_algo.chapter_hashing;
public class hash_map { public class hash_map {

View File

@ -4,9 +4,6 @@
* Author: zjkung1123 (zjkung1123@gmail.com) * Author: zjkung1123 (zjkung1123@gmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_heap; namespace hello_algo.chapter_heap;
public class heap { public class heap {

View File

@ -4,9 +4,6 @@
* Author: zjkung1123 (zjkung1123@gmail.com) * Author: zjkung1123 (zjkung1123@gmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_heap; namespace hello_algo.chapter_heap;
/* 大顶堆 */ /* 大顶堆 */

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_searching; namespace hello_algo.chapter_searching;
public class hashing_search { public class hashing_search {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_searching; namespace hello_algo.chapter_searching;
public class leetcode_two_sum { public class leetcode_two_sum {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_searching; namespace hello_algo.chapter_searching;
public class linear_search { public class linear_search {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class bubble_sort { public class bubble_sort {

View File

@ -4,8 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class bucket_sort { public class bucket_sort {

View File

@ -4,8 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class counting_sort { public class counting_sort {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class insertion_sort { public class insertion_sort {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class merge_sort { public class merge_sort {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
class QuickSort { class QuickSort {

View File

@ -4,8 +4,6 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_sorting; namespace hello_algo.chapter_sorting;
public class radix_sort { public class radix_sort {

View File

@ -4,151 +4,149 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
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 readonly int[] nums; // 用于存储双向队列元素的数组 private int front; // 队首指针,指向队首元素
private int front; // 队首指针,指向队首元素 private int queSize; // 双向队列长度
private int queSize; // 双向队列长度
/* 构造方法 */ /* 构造方法 */
public ArrayDeque(int capacity) { public ArrayDeque(int capacity) {
this.nums = new int[capacity]; this.nums = new int[capacity];
front = queSize = 0; front = queSize = 0;
}
/* 获取双向队列的容量 */
public int capacity() {
return nums.Length;
}
/* 获取双向队列的长度 */
public int size() {
return queSize;
}
/* 判断双向队列是否为空 */
public bool isEmpty() {
return queSize == 0;
}
/* 计算环形数组索引 */
private int index(int i) {
// 通过取余操作实现数组首尾相连
// 当 i 越过数组尾部后,回到头部
// 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity();
}
/* 队首入队 */
public void pushFirst(int num) {
if (queSize == capacity()) {
Console.WriteLine("双向队列已满");
return;
}
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部后回到尾部
front = index(front - 1);
// 将 num 添加至队首
nums[front] = num;
queSize++;
}
/* 队尾入队 */
public void pushLast(int num) {
if (queSize == capacity()) {
Console.WriteLine("双向队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
int rear = index(front + queSize);
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 队首出队 */
public int popFirst() {
int num = peekFirst();
// 队首指针向后移动一位
front = index(front + 1);
queSize--;
return num;
}
/* 队尾出队 */
public int popLast() {
int num = peekLast();
queSize--;
return num;
}
/* 访问队首元素 */
public int peekFirst() {
if (isEmpty()) {
throw new InvalidOperationException();
}
return nums[front];
}
/* 访问队尾元素 */
public int peekLast() {
if (isEmpty()) {
throw new InvalidOperationException();
}
// 计算尾元素索引
int last = index(front + queSize - 1);
return nums[last];
}
/* 返回数组用于打印 */
public int[] toArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
}
return res;
}
} }
public class array_deque { /* 获取双向队列的容量 */
[Test] public int capacity() {
public void Test() { return nums.Length;
/* 初始化双向队列 */ }
ArrayDeque deque = new ArrayDeque(10);
deque.pushLast(3);
deque.pushLast(2);
deque.pushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
/* 访问元素 */ /* 获取双向队列的长度 */
int peekFirst = deque.peekFirst(); public int size() {
Console.WriteLine("队首元素 peekFirst = " + peekFirst); return queSize;
int peekLast = deque.peekLast(); }
Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素入队 */ /* 判断双向队列是否为空 */
deque.pushLast(4); public bool isEmpty() {
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray())); return queSize == 0;
deque.pushFirst(1); }
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
/* 元素出队 */ /* 计算环形数组索引 */
int popLast = deque.popLast(); private int index(int i) {
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray())); // 通过取余操作实现数组首尾相连
int popFirst = deque.popFirst(); // 当 i 越过数组尾部后,回到头部
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray())); // 当 i 越过数组头部后,回到尾部
return (i + capacity()) % capacity();
}
/* 获取双向队列的长度 */ /* 队首入队 */
int size = deque.size(); public void pushFirst(int num) {
Console.WriteLine("双向队列长度 size = " + size); if (queSize == capacity()) {
Console.WriteLine("双向队列已满");
/* 判断双向队列是否为空 */ return;
bool isEmpty = deque.isEmpty();
Console.WriteLine("双向队列是否为空 = " + isEmpty);
} }
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部后回到尾部
front = index(front - 1);
// 将 num 添加至队首
nums[front] = num;
queSize++;
}
/* 队尾入队 */
public void pushLast(int num) {
if (queSize == capacity()) {
Console.WriteLine("双向队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
int rear = index(front + queSize);
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 队首出队 */
public int popFirst() {
int num = peekFirst();
// 队首指针向后移动一位
front = index(front + 1);
queSize--;
return num;
}
/* 队尾出队 */
public int popLast() {
int num = peekLast();
queSize--;
return num;
}
/* 访问队首元素 */
public int peekFirst() {
if (isEmpty()) {
throw new InvalidOperationException();
}
return nums[front];
}
/* 访问队尾元素 */
public int peekLast() {
if (isEmpty()) {
throw new InvalidOperationException();
}
// 计算尾元素索引
int last = index(front + queSize - 1);
return nums[last];
}
/* 返回数组用于打印 */
public int[] toArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[index(j)];
}
return res;
}
}
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()));
/* 访问元素 */
int peekFirst = deque.peekFirst();
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
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()));
/* 元素出队 */
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();
Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */
bool isEmpty = deque.isEmpty();
Console.WriteLine("双向队列是否为空 = " + isEmpty);
} }
} }

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
/* 基于环形数组实现的队列 */ /* 基于环形数组实现的队列 */

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
/* 基于数组实现的栈 */ /* 基于数组实现的栈 */

View File

@ -4,8 +4,6 @@
* Author: moonache (microin1301@outlook.com) * Author: moonache (microin1301@outlook.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
public class deque { public class deque {

View File

@ -4,183 +4,181 @@
* Author: hpstory (hpstory1024@163.com) * Author: hpstory (hpstory1024@163.com)
*/ */
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 int val; // 节点值 public ListNode? next; // 后继节点引用(指针)
public ListNode? next; // 后继节点引用(指针) public ListNode? prev; // 前驱节点引用(指针)
public ListNode? prev; // 前驱节点引用(指针)
public ListNode(int val) { public ListNode(int val) {
this.val = val; this.val = val;
prev = null; prev = null;
next = null; next = null;
} }
} }
/* 基于双向链表实现的双向队列 */ /* 基于双向链表实现的双向队列 */
public class LinkedListDeque { public class LinkedListDeque {
private ListNode? front, rear; // 头节点 front, 尾节点 rear private ListNode? front, rear; // 头节点 front, 尾节点 rear
private int queSize = 0; // 双向队列的长度 private int queSize = 0; // 双向队列的长度
public LinkedListDeque() { public LinkedListDeque() {
front = null; front = null;
rear = null; rear = null;
} }
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */
public int size() { public int size() {
return queSize; return queSize;
} }
/* 判断双向队列是否为空 */ /* 判断双向队列是否为空 */
public bool isEmpty() { public bool isEmpty() {
return size() == 0; return size() == 0;
} }
/* 入队操作 */ /* 入队操作 */
private void push(int num, bool isFront) { private void push(int num, bool isFront) {
ListNode node = new ListNode(num); ListNode node = new ListNode(num);
// 若链表为空,则令 front, rear 都指向 node // 若链表为空,则令 front, rear 都指向 node
if (isEmpty()) { if (isEmpty()) {
front = node; front = node;
rear = node; rear = node;
} }
// 队首入队操作 // 队首入队操作
else if (isFront) { else if (isFront) {
// 将 node 添加至链表头部 // 将 node 添加至链表头部
front.prev = node; front.prev = node;
node.next = front; node.next = front;
front = node; // 更新头节点 front = node; // 更新头节点
} }
// 队尾入队操作 // 队尾入队操作
else { else {
// 将 node 添加至链表尾部 // 将 node 添加至链表尾部
rear.next = node; rear.next = node;
node.prev = rear; node.prev = rear;
rear = node; // 更新尾节点 rear = node; // 更新尾节点
} }
queSize++; // 更新队列长度 queSize++; // 更新队列长度
} }
/* 队首入队 */ /* 队首入队 */
public void pushFirst(int num) { public void pushFirst(int num) {
push(num, true); push(num, true);
} }
/* 队尾入队 */ /* 队尾入队 */
public void pushLast(int num) { public void pushLast(int num) {
push(num, false); push(num, false);
} }
/* 出队操作 */ /* 出队操作 */
private int? pop(bool isFront) { private int? pop(bool isFront) {
// 若队列为空,直接返回 null // 若队列为空,直接返回 null
if (isEmpty()) { if (isEmpty()) {
return null; return null;
} }
int val; int val;
// 队首出队操作 // 队首出队操作
if (isFront) { if (isFront) {
val = front.val; // 暂存头节点值 val = front.val; // 暂存头节点值
// 删除头节点 // 删除头节点
ListNode fNext = front.next; ListNode fNext = front.next;
if (fNext != null) { if (fNext != null) {
fNext.prev = null; fNext.prev = null;
front.next = null; front.next = null;
} }
front = fNext; // 更新头节点 front = fNext; // 更新头节点
} }
// 队尾出队操作 // 队尾出队操作
else { else {
val = rear.val; // 暂存尾节点值 val = rear.val; // 暂存尾节点值
// 删除尾节点 // 删除尾节点
ListNode rPrev = rear.prev; ListNode rPrev = rear.prev;
if (rPrev != null) { if (rPrev != null) {
rPrev.next = null; rPrev.next = null;
rear.prev = null; rear.prev = null;
} }
rear = rPrev; // 更新尾节点 rear = rPrev; // 更新尾节点
} }
queSize--; // 更新队列长度 queSize--; // 更新队列长度
return val; return val;
} }
/* 队首出队 */ /* 队首出队 */
public int? popFirst() { public int? popFirst() {
return pop(true); return pop(true);
} }
/* 队尾出队 */ /* 队尾出队 */
public int? popLast() { public int? popLast() {
return pop(false); return pop(false);
} }
/* 访问队首元素 */ /* 访问队首元素 */
public int? peekFirst() { public int? peekFirst() {
return isEmpty() ? null : front.val; return isEmpty() ? null : front.val;
} }
/* 访问队尾元素 */ /* 访问队尾元素 */
public int? peekLast() { public int? peekLast() {
return isEmpty() ? null : rear.val; return isEmpty() ? null : rear.val;
} }
/* 返回数组用于打印 */ /* 返回数组用于打印 */
public int[] toArray() { public int[] toArray() {
ListNode node = front; ListNode node = front;
int[] res = new int[size()]; 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; res[i] = node.val;
node = node.next; node = node.next;
} }
return res; return res;
} }
} }
public class linkedlist_deque { public class linkedlist_deque {
[Test] [Test]
public void Test() { public void Test() {
/* 初始化双向队列 */ /* 初始化双向队列 */
LinkedListDeque deque = new LinkedListDeque(); LinkedListDeque deque = new LinkedListDeque();
deque.pushLast(3); deque.pushLast(3);
deque.pushLast(2); deque.pushLast(2);
deque.pushLast(5); deque.pushLast(5);
Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray())); Console.WriteLine("双向队列 deque = " + string.Join(" ", deque.toArray()));
/* 访问元素 */ /* 访问元素 */
int? peekFirst = deque.peekFirst(); int? peekFirst = deque.peekFirst();
Console.WriteLine("队首元素 peekFirst = " + peekFirst); Console.WriteLine("队首元素 peekFirst = " + peekFirst);
int? peekLast = deque.peekLast(); int? peekLast = deque.peekLast();
Console.WriteLine("队尾元素 peekLast = " + peekLast); Console.WriteLine("队尾元素 peekLast = " + peekLast);
/* 元素入队 */ /* 元素入队 */
deque.pushLast(4); deque.pushLast(4);
Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray())); Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray()));
deque.pushFirst(1); deque.pushFirst(1);
Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray())); Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray()));
/* 元素出队 */ /* 元素出队 */
int? popLast = deque.popLast(); int? popLast = deque.popLast();
Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray())); Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray()));
int? popFirst = deque.popFirst(); int? popFirst = deque.popFirst();
Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray())); Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray()));
/* 获取双向队列的长度 */ /* 获取双向队列的长度 */
int size = deque.size(); int size = deque.size();
Console.WriteLine("双向队列长度 size = " + size); Console.WriteLine("双向队列长度 size = " + size);
/* 判断双向队列是否为空 */ /* 判断双向队列是否为空 */
bool isEmpty = deque.isEmpty(); bool isEmpty = deque.isEmpty();
Console.WriteLine("双向队列是否为空 = " + isEmpty); Console.WriteLine("双向队列是否为空 = " + isEmpty);
}
} }
} }

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的队列 */ /* 基于链表实现的队列 */

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
/* 基于链表实现的栈 */ /* 基于链表实现的栈 */

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
public class queue { public class queue {

View File

@ -4,8 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue; namespace hello_algo.chapter_stack_and_queue;
public class stack { public class stack {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
/* AVL 树 */ /* AVL 树 */

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
class BinarySearchTree { class BinarySearchTree {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
public class binary_tree { public class binary_tree {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
public class binary_tree_bfs { public class binary_tree_bfs {

View File

@ -4,9 +4,6 @@
* Author: haptear (haptear@hotmail.com) * Author: haptear (haptear@hotmail.com)
*/ */
using hello_algo.utils;
using NUnit.Framework;
namespace hello_algo.chapter_tree; namespace hello_algo.chapter_tree;
public class binary_tree_dfs { public class binary_tree_dfs {