From 40319e7f3014068dd54c727f02efe62198126d36 Mon Sep 17 00:00:00 2001 From: hpstory <33348162+hpstory@users.noreply.github.com> Date: Sat, 6 May 2023 22:48:02 +0800 Subject: [PATCH] refactor: use global using to reduce usings in each file (#486) --- codes/csharp/GlobalUsing.cs | 2 + .../chapter_array_and_linkedlist/array.cs | 2 - .../linked_list.cs | 3 - .../chapter_array_and_linkedlist/list.cs | 2 - .../chapter_array_and_linkedlist/my_list.cs | 2 - codes/csharp/chapter_backtracking/n_queens.cs | 3 - .../chapter_backtracking/permutations_i.cs | 3 - .../chapter_backtracking/permutations_ii.cs | 3 - .../preorder_traversal_i_compact.cs | 3 - .../preorder_traversal_ii_compact.cs | 3 - .../preorder_traversal_iii_compact.cs | 3 - .../preorder_traversal_iii_template.cs | 3 - .../chapter_binary_search/binary_search.cs | 2 - .../space_complexity.cs | 3 - .../time_complexity.cs | 2 - .../worst_best_time_complexity.cs | 2 - .../chapter_graph/graph_adjacency_list.cs | 3 - .../chapter_graph/graph_adjacency_matrix.cs | 3 - codes/csharp/chapter_graph/graph_bfs.cs | 3 - codes/csharp/chapter_graph/graph_dfs.cs | 3 - .../csharp/chapter_hashing/array_hash_map.cs | 2 - codes/csharp/chapter_hashing/hash_map.cs | 3 - codes/csharp/chapter_heap/heap.cs | 3 - codes/csharp/chapter_heap/my_heap.cs | 3 - .../chapter_searching/hashing_search.cs | 3 - .../chapter_searching/leetcode_two_sum.cs | 2 - .../csharp/chapter_searching/linear_search.cs | 3 - codes/csharp/chapter_sorting/bubble_sort.cs | 2 - codes/csharp/chapter_sorting/bucket_sort.cs | 2 - codes/csharp/chapter_sorting/counting_sort.cs | 2 - .../csharp/chapter_sorting/insertion_sort.cs | 2 - codes/csharp/chapter_sorting/merge_sort.cs | 2 - codes/csharp/chapter_sorting/quick_sort.cs | 2 - codes/csharp/chapter_sorting/radix_sort.cs | 2 - .../chapter_stack_and_queue/array_deque.cs | 272 +++++++------- .../chapter_stack_and_queue/array_queue.cs | 2 - .../chapter_stack_and_queue/array_stack.cs | 2 - codes/csharp/chapter_stack_and_queue/deque.cs | 2 - .../linkedlist_deque.cs | 350 +++++++++--------- .../linkedlist_queue.cs | 3 - .../linkedlist_stack.cs | 3 - codes/csharp/chapter_stack_and_queue/queue.cs | 2 - codes/csharp/chapter_stack_and_queue/stack.cs | 2 - codes/csharp/chapter_tree/avl_tree.cs | 3 - .../csharp/chapter_tree/binary_search_tree.cs | 3 - codes/csharp/chapter_tree/binary_tree.cs | 3 - codes/csharp/chapter_tree/binary_tree_bfs.cs | 3 - codes/csharp/chapter_tree/binary_tree_dfs.cs | 3 - 48 files changed, 311 insertions(+), 428 deletions(-) create mode 100644 codes/csharp/GlobalUsing.cs diff --git a/codes/csharp/GlobalUsing.cs b/codes/csharp/GlobalUsing.cs new file mode 100644 index 00000000..fb4c84fc --- /dev/null +++ b/codes/csharp/GlobalUsing.cs @@ -0,0 +1,2 @@ +global using NUnit.Framework; +global using hello_algo.utils; \ No newline at end of file diff --git a/codes/csharp/chapter_array_and_linkedlist/array.cs b/codes/csharp/chapter_array_and_linkedlist/array.cs index 292a8aa4..fdacd18a 100644 --- a/codes/csharp/chapter_array_and_linkedlist/array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/array.cs @@ -2,8 +2,6 @@ // Created Time: 2022-12-14 // Author: mingXta (1195669834@qq.com) -using NUnit.Framework; - namespace hello_algo.chapter_array_and_linkedlist; public class array { diff --git a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs index fdaf5ea1..9cd86b9a 100644 --- a/codes/csharp/chapter_array_and_linkedlist/linked_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs @@ -2,9 +2,6 @@ // Created Time: 2022-12-16 // Author: mingXta (1195669834@qq.com) -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_array_and_linkedlist; public class linked_list { diff --git a/codes/csharp/chapter_array_and_linkedlist/list.cs b/codes/csharp/chapter_array_and_linkedlist/list.cs index 1fdde944..1a95c234 100644 --- a/codes/csharp/chapter_array_and_linkedlist/list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/list.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_array_and_linkedlist; public class list { diff --git a/codes/csharp/chapter_array_and_linkedlist/my_list.cs b/codes/csharp/chapter_array_and_linkedlist/my_list.cs index 10b1b4c0..0709cb68 100644 --- a/codes/csharp/chapter_array_and_linkedlist/my_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/my_list.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_array_and_linkedlist; /* 列表类简易实现 */ diff --git a/codes/csharp/chapter_backtracking/n_queens.cs b/codes/csharp/chapter_backtracking/n_queens.cs index 167d33a5..bc0cd9fd 100644 --- a/codes/csharp/chapter_backtracking/n_queens.cs +++ b/codes/csharp/chapter_backtracking/n_queens.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class n_queens { diff --git a/codes/csharp/chapter_backtracking/permutations_i.cs b/codes/csharp/chapter_backtracking/permutations_i.cs index 3553151b..4f54ccae 100644 --- a/codes/csharp/chapter_backtracking/permutations_i.cs +++ b/codes/csharp/chapter_backtracking/permutations_i.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class permutations_i { diff --git a/codes/csharp/chapter_backtracking/permutations_ii.cs b/codes/csharp/chapter_backtracking/permutations_ii.cs index e0ffeacb..d56eb60d 100644 --- a/codes/csharp/chapter_backtracking/permutations_ii.cs +++ b/codes/csharp/chapter_backtracking/permutations_ii.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class permutations_ii { diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs index 3d6e48e9..82b7d607 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_i_compact.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class preorder_traversal_i_compact { diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs index 0512a144..47f29eff 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_ii_compact.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class preorder_traversal_ii_compact { diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs index 606dd97a..c17a6dd2 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class preorder_traversal_iii_compact { diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs index 32824d69..8b850cf8 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_template.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_backtracking; public class preorder_traversal_iii_template { diff --git a/codes/csharp/chapter_binary_search/binary_search.cs b/codes/csharp/chapter_binary_search/binary_search.cs index 6e9bbc97..6712c5e7 100644 --- a/codes/csharp/chapter_binary_search/binary_search.cs +++ b/codes/csharp/chapter_binary_search/binary_search.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_binary_search; public class binary_search { diff --git a/codes/csharp/chapter_computational_complexity/space_complexity.cs b/codes/csharp/chapter_computational_complexity/space_complexity.cs index 976b3d28..993851a8 100644 --- a/codes/csharp/chapter_computational_complexity/space_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/space_complexity.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_computational_complexity; public class space_complexity { diff --git a/codes/csharp/chapter_computational_complexity/time_complexity.cs b/codes/csharp/chapter_computational_complexity/time_complexity.cs index f0bcb3ba..d930cc0b 100644 --- a/codes/csharp/chapter_computational_complexity/time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/time_complexity.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_computational_complexity; public class time_complexity { diff --git a/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs b/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs index 67328d19..c2d3535f 100644 --- a/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/worst_best_time_complexity.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_computational_complexity; public class worst_best_time_complexity { diff --git a/codes/csharp/chapter_graph/graph_adjacency_list.cs b/codes/csharp/chapter_graph/graph_adjacency_list.cs index 44bec3b9..ae66eec1 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_list.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_list.cs @@ -4,9 +4,6 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_graph; /* 基于邻接表实现的无向图类 */ diff --git a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs index 7ccee094..afc94d5f 100644 --- a/codes/csharp/chapter_graph/graph_adjacency_matrix.cs +++ b/codes/csharp/chapter_graph/graph_adjacency_matrix.cs @@ -4,9 +4,6 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_graph; /* 基于邻接矩阵实现的无向图类 */ diff --git a/codes/csharp/chapter_graph/graph_bfs.cs b/codes/csharp/chapter_graph/graph_bfs.cs index 286c13f9..518eb646 100644 --- a/codes/csharp/chapter_graph/graph_bfs.cs +++ b/codes/csharp/chapter_graph/graph_bfs.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_graph; public class graph_bfs { diff --git a/codes/csharp/chapter_graph/graph_dfs.cs b/codes/csharp/chapter_graph/graph_dfs.cs index f1c3d9d2..25629151 100644 --- a/codes/csharp/chapter_graph/graph_dfs.cs +++ b/codes/csharp/chapter_graph/graph_dfs.cs @@ -4,9 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_graph; public class graph_dfs { diff --git a/codes/csharp/chapter_hashing/array_hash_map.cs b/codes/csharp/chapter_hashing/array_hash_map.cs index c92dd098..988c7e88 100644 --- a/codes/csharp/chapter_hashing/array_hash_map.cs +++ b/codes/csharp/chapter_hashing/array_hash_map.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_hashing; /* 键值对 int->string */ diff --git a/codes/csharp/chapter_hashing/hash_map.cs b/codes/csharp/chapter_hashing/hash_map.cs index 6837deb0..0fa7315d 100644 --- a/codes/csharp/chapter_hashing/hash_map.cs +++ b/codes/csharp/chapter_hashing/hash_map.cs @@ -5,9 +5,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_hashing; public class hash_map { diff --git a/codes/csharp/chapter_heap/heap.cs b/codes/csharp/chapter_heap/heap.cs index f005ff47..c30f9864 100644 --- a/codes/csharp/chapter_heap/heap.cs +++ b/codes/csharp/chapter_heap/heap.cs @@ -4,9 +4,6 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_heap; public class heap { diff --git a/codes/csharp/chapter_heap/my_heap.cs b/codes/csharp/chapter_heap/my_heap.cs index 0c5421ad..5918787c 100644 --- a/codes/csharp/chapter_heap/my_heap.cs +++ b/codes/csharp/chapter_heap/my_heap.cs @@ -4,9 +4,6 @@ * Author: zjkung1123 (zjkung1123@gmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_heap; /* 大顶堆 */ diff --git a/codes/csharp/chapter_searching/hashing_search.cs b/codes/csharp/chapter_searching/hashing_search.cs index 4f15b4eb..03d242b7 100644 --- a/codes/csharp/chapter_searching/hashing_search.cs +++ b/codes/csharp/chapter_searching/hashing_search.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_searching; public class hashing_search { diff --git a/codes/csharp/chapter_searching/leetcode_two_sum.cs b/codes/csharp/chapter_searching/leetcode_two_sum.cs index 9e614c8c..e6cc3fd5 100644 --- a/codes/csharp/chapter_searching/leetcode_two_sum.cs +++ b/codes/csharp/chapter_searching/leetcode_two_sum.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_searching; public class leetcode_two_sum { diff --git a/codes/csharp/chapter_searching/linear_search.cs b/codes/csharp/chapter_searching/linear_search.cs index b30b3829..eb5d989e 100644 --- a/codes/csharp/chapter_searching/linear_search.cs +++ b/codes/csharp/chapter_searching/linear_search.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_searching; public class linear_search { diff --git a/codes/csharp/chapter_sorting/bubble_sort.cs b/codes/csharp/chapter_sorting/bubble_sort.cs index 5ed88362..6e4d0c25 100644 --- a/codes/csharp/chapter_sorting/bubble_sort.cs +++ b/codes/csharp/chapter_sorting/bubble_sort.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class bubble_sort { diff --git a/codes/csharp/chapter_sorting/bucket_sort.cs b/codes/csharp/chapter_sorting/bucket_sort.cs index 8a1aac4f..ab02924d 100644 --- a/codes/csharp/chapter_sorting/bucket_sort.cs +++ b/codes/csharp/chapter_sorting/bucket_sort.cs @@ -4,8 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class bucket_sort { diff --git a/codes/csharp/chapter_sorting/counting_sort.cs b/codes/csharp/chapter_sorting/counting_sort.cs index 19b5815a..dab31c8f 100644 --- a/codes/csharp/chapter_sorting/counting_sort.cs +++ b/codes/csharp/chapter_sorting/counting_sort.cs @@ -4,8 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class counting_sort { diff --git a/codes/csharp/chapter_sorting/insertion_sort.cs b/codes/csharp/chapter_sorting/insertion_sort.cs index ea5f014f..a6a50982 100644 --- a/codes/csharp/chapter_sorting/insertion_sort.cs +++ b/codes/csharp/chapter_sorting/insertion_sort.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class insertion_sort { diff --git a/codes/csharp/chapter_sorting/merge_sort.cs b/codes/csharp/chapter_sorting/merge_sort.cs index fc84e6c5..b5837935 100644 --- a/codes/csharp/chapter_sorting/merge_sort.cs +++ b/codes/csharp/chapter_sorting/merge_sort.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class merge_sort { diff --git a/codes/csharp/chapter_sorting/quick_sort.cs b/codes/csharp/chapter_sorting/quick_sort.cs index c1a4929c..60fd8119 100644 --- a/codes/csharp/chapter_sorting/quick_sort.cs +++ b/codes/csharp/chapter_sorting/quick_sort.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; class QuickSort { diff --git a/codes/csharp/chapter_sorting/radix_sort.cs b/codes/csharp/chapter_sorting/radix_sort.cs index 8421fdc8..92156d7a 100644 --- a/codes/csharp/chapter_sorting/radix_sort.cs +++ b/codes/csharp/chapter_sorting/radix_sort.cs @@ -4,8 +4,6 @@ * Author: hpstory (hpstory1024@163.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_sorting; public class radix_sort { diff --git a/codes/csharp/chapter_stack_and_queue/array_deque.cs b/codes/csharp/chapter_stack_and_queue/array_deque.cs index 083c477d..31228672 100644 --- a/codes/csharp/chapter_stack_and_queue/array_deque.cs +++ b/codes/csharp/chapter_stack_and_queue/array_deque.cs @@ -4,151 +4,149 @@ * 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 { - private readonly int[] nums; // 用于存储双向队列元素的数组 - private int front; // 队首指针,指向队首元素 - private int queSize; // 双向队列长度 +/* 基于环形数组实现的双向队列 */ +public class ArrayDeque { + private readonly int[] nums; // 用于存储双向队列元素的数组 + private int front; // 队首指针,指向队首元素 + private int queSize; // 双向队列长度 - /* 构造方法 */ - public ArrayDeque(int capacity) { - this.nums = new int[capacity]; - 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 ArrayDeque(int capacity) { + this.nums = new int[capacity]; + front = queSize = 0; } - 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())); + /* 获取双向队列的容量 */ + public int capacity() { + return nums.Length; + } - /* 访问元素 */ - int peekFirst = deque.peekFirst(); - Console.WriteLine("队首元素 peekFirst = " + peekFirst); - int peekLast = deque.peekLast(); - Console.WriteLine("队尾元素 peekLast = " + peekLast); + /* 获取双向队列的长度 */ + public int size() { + return queSize; + } - /* 元素入队 */ - deque.pushLast(4); - Console.WriteLine("元素 4 队尾入队后 deque = " + string.Join(" ", deque.toArray())); - deque.pushFirst(1); - Console.WriteLine("元素 1 队首入队后 deque = " + string.Join(" ", deque.toArray())); + /* 判断双向队列是否为空 */ + public bool isEmpty() { + return queSize == 0; + } - /* 元素出队 */ - int popLast = deque.popLast(); - Console.WriteLine("队尾出队元素 = " + popLast + ",队尾出队后 deque = " + string.Join(" ", deque.toArray())); - int popFirst = deque.popFirst(); - Console.WriteLine("队首出队元素 = " + popFirst + ",队首出队后 deque = " + string.Join(" ", deque.toArray())); + /* 计算环形数组索引 */ + private int index(int i) { + // 通过取余操作实现数组首尾相连 + // 当 i 越过数组尾部后,回到头部 + // 当 i 越过数组头部后,回到尾部 + return (i + capacity()) % capacity(); + } - /* 获取双向队列的长度 */ - int size = deque.size(); - Console.WriteLine("双向队列长度 size = " + size); - - /* 判断双向队列是否为空 */ - bool isEmpty = deque.isEmpty(); - Console.WriteLine("双向队列是否为空 = " + isEmpty); + /* 队首入队 */ + 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 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); } } diff --git a/codes/csharp/chapter_stack_and_queue/array_queue.cs b/codes/csharp/chapter_stack_and_queue/array_queue.cs index e296b2e8..23143521 100644 --- a/codes/csharp/chapter_stack_and_queue/array_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/array_queue.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; /* 基于环形数组实现的队列 */ diff --git a/codes/csharp/chapter_stack_and_queue/array_stack.cs b/codes/csharp/chapter_stack_and_queue/array_stack.cs index b74f922d..2e714817 100644 --- a/codes/csharp/chapter_stack_and_queue/array_stack.cs +++ b/codes/csharp/chapter_stack_and_queue/array_stack.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; /* 基于数组实现的栈 */ diff --git a/codes/csharp/chapter_stack_and_queue/deque.cs b/codes/csharp/chapter_stack_and_queue/deque.cs index f55f3667..4df51b76 100644 --- a/codes/csharp/chapter_stack_and_queue/deque.cs +++ b/codes/csharp/chapter_stack_and_queue/deque.cs @@ -4,8 +4,6 @@ * Author: moonache (microin1301@outlook.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; public class deque { diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs index 87f81819..893d36c1 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs @@ -4,183 +4,181 @@ * 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 int val; // 节点值 - public ListNode? next; // 后继节点引用(指针) - public ListNode? prev; // 前驱节点引用(指针) +/* 双向链表节点 */ +public class ListNode { + public int val; // 节点值 + public ListNode? next; // 后继节点引用(指针) + public ListNode? prev; // 前驱节点引用(指针) - public ListNode(int val) { - this.val = val; - prev = null; - next = null; - } - } - - /* 基于双向链表实现的双向队列 */ - public class LinkedListDeque { - private ListNode? front, rear; // 头节点 front, 尾节点 rear - private int queSize = 0; // 双向队列的长度 - - public LinkedListDeque() { - front = null; - rear = null; - } - - /* 获取双向队列的长度 */ - public int size() { - return queSize; - } - - /* 判断双向队列是否为空 */ - public bool isEmpty() { - return size() == 0; - } - - /* 入队操作 */ - private void push(int num, bool isFront) { - ListNode node = new ListNode(num); - // 若链表为空,则令 front, rear 都指向 node - if (isEmpty()) { - front = node; - rear = node; - } - // 队首入队操作 - else if (isFront) { - // 将 node 添加至链表头部 - front.prev = node; - node.next = front; - front = node; // 更新头节点 - } - // 队尾入队操作 - else { - // 将 node 添加至链表尾部 - rear.next = node; - node.prev = rear; - rear = node; // 更新尾节点 - } - - queSize++; // 更新队列长度 - } - - /* 队首入队 */ - public void pushFirst(int num) { - push(num, true); - } - - /* 队尾入队 */ - public void pushLast(int num) { - push(num, false); - } - - /* 出队操作 */ - private int? pop(bool isFront) { - // 若队列为空,直接返回 null - if (isEmpty()) { - return null; - } - - int val; - // 队首出队操作 - if (isFront) { - val = front.val; // 暂存头节点值 - // 删除头节点 - ListNode fNext = front.next; - if (fNext != null) { - fNext.prev = null; - front.next = null; - } - - front = fNext; // 更新头节点 - } - // 队尾出队操作 - else { - val = rear.val; // 暂存尾节点值 - // 删除尾节点 - ListNode rPrev = rear.prev; - if (rPrev != null) { - rPrev.next = null; - rear.prev = null; - } - - rear = rPrev; // 更新尾节点 - } - - queSize--; // 更新队列长度 - return val; - } - - /* 队首出队 */ - public int? popFirst() { - return pop(true); - } - - /* 队尾出队 */ - public int? popLast() { - return pop(false); - } - - /* 访问队首元素 */ - public int? peekFirst() { - return isEmpty() ? null : front.val; - } - - /* 访问队尾元素 */ - public int? peekLast() { - return isEmpty() ? null : rear.val; - } - - /* 返回数组用于打印 */ - 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; - } - - return res; - } - } - - 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())); - - /* 访问元素 */ - 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); - } + public ListNode(int val) { + this.val = val; + prev = null; + next = null; + } +} + +/* 基于双向链表实现的双向队列 */ +public class LinkedListDeque { + private ListNode? front, rear; // 头节点 front, 尾节点 rear + private int queSize = 0; // 双向队列的长度 + + public LinkedListDeque() { + front = null; + rear = null; + } + + /* 获取双向队列的长度 */ + public int size() { + return queSize; + } + + /* 判断双向队列是否为空 */ + public bool isEmpty() { + return size() == 0; + } + + /* 入队操作 */ + private void push(int num, bool isFront) { + ListNode node = new ListNode(num); + // 若链表为空,则令 front, rear 都指向 node + if (isEmpty()) { + front = node; + rear = node; + } + // 队首入队操作 + else if (isFront) { + // 将 node 添加至链表头部 + front.prev = node; + node.next = front; + front = node; // 更新头节点 + } + // 队尾入队操作 + else { + // 将 node 添加至链表尾部 + rear.next = node; + node.prev = rear; + rear = node; // 更新尾节点 + } + + queSize++; // 更新队列长度 + } + + /* 队首入队 */ + public void pushFirst(int num) { + push(num, true); + } + + /* 队尾入队 */ + public void pushLast(int num) { + push(num, false); + } + + /* 出队操作 */ + private int? pop(bool isFront) { + // 若队列为空,直接返回 null + if (isEmpty()) { + return null; + } + + int val; + // 队首出队操作 + if (isFront) { + val = front.val; // 暂存头节点值 + // 删除头节点 + ListNode fNext = front.next; + if (fNext != null) { + fNext.prev = null; + front.next = null; + } + + front = fNext; // 更新头节点 + } + // 队尾出队操作 + else { + val = rear.val; // 暂存尾节点值 + // 删除尾节点 + ListNode rPrev = rear.prev; + if (rPrev != null) { + rPrev.next = null; + rear.prev = null; + } + + rear = rPrev; // 更新尾节点 + } + + queSize--; // 更新队列长度 + return val; + } + + /* 队首出队 */ + public int? popFirst() { + return pop(true); + } + + /* 队尾出队 */ + public int? popLast() { + return pop(false); + } + + /* 访问队首元素 */ + public int? peekFirst() { + return isEmpty() ? null : front.val; + } + + /* 访问队尾元素 */ + public int? peekLast() { + return isEmpty() ? null : rear.val; + } + + /* 返回数组用于打印 */ + 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; + } + + return res; + } +} + +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())); + + /* 访问元素 */ + 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); } } diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs index 12c8f80b..55824b4a 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; /* 基于链表实现的队列 */ diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs index f4dfc2e0..7014a53e 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; /* 基于链表实现的栈 */ diff --git a/codes/csharp/chapter_stack_and_queue/queue.cs b/codes/csharp/chapter_stack_and_queue/queue.cs index a6bfce74..121a0aa6 100644 --- a/codes/csharp/chapter_stack_and_queue/queue.cs +++ b/codes/csharp/chapter_stack_and_queue/queue.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; public class queue { diff --git a/codes/csharp/chapter_stack_and_queue/stack.cs b/codes/csharp/chapter_stack_and_queue/stack.cs index f6643407..0c86c4fe 100644 --- a/codes/csharp/chapter_stack_and_queue/stack.cs +++ b/codes/csharp/chapter_stack_and_queue/stack.cs @@ -4,8 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using NUnit.Framework; - namespace hello_algo.chapter_stack_and_queue; public class stack { diff --git a/codes/csharp/chapter_tree/avl_tree.cs b/codes/csharp/chapter_tree/avl_tree.cs index 60485d3b..9c13a91d 100644 --- a/codes/csharp/chapter_tree/avl_tree.cs +++ b/codes/csharp/chapter_tree/avl_tree.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_tree; /* AVL 树 */ diff --git a/codes/csharp/chapter_tree/binary_search_tree.cs b/codes/csharp/chapter_tree/binary_search_tree.cs index 39136998..e85dfc4c 100644 --- a/codes/csharp/chapter_tree/binary_search_tree.cs +++ b/codes/csharp/chapter_tree/binary_search_tree.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_tree; class BinarySearchTree { diff --git a/codes/csharp/chapter_tree/binary_tree.cs b/codes/csharp/chapter_tree/binary_tree.cs index 4dc0e80b..b132cd47 100644 --- a/codes/csharp/chapter_tree/binary_tree.cs +++ b/codes/csharp/chapter_tree/binary_tree.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_tree; public class binary_tree { diff --git a/codes/csharp/chapter_tree/binary_tree_bfs.cs b/codes/csharp/chapter_tree/binary_tree_bfs.cs index f0548198..590f1555 100644 --- a/codes/csharp/chapter_tree/binary_tree_bfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_bfs.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_tree; public class binary_tree_bfs { diff --git a/codes/csharp/chapter_tree/binary_tree_dfs.cs b/codes/csharp/chapter_tree/binary_tree_dfs.cs index b0935a55..bb11c657 100644 --- a/codes/csharp/chapter_tree/binary_tree_dfs.cs +++ b/codes/csharp/chapter_tree/binary_tree_dfs.cs @@ -4,9 +4,6 @@ * Author: haptear (haptear@hotmail.com) */ -using hello_algo.utils; -using NUnit.Framework; - namespace hello_algo.chapter_tree; public class binary_tree_dfs {