From 47bf6729a420d8d17ee37f6d7f8a00e3466d431d Mon Sep 17 00:00:00 2001 From: ming <1195669834@qq.com> Date: Fri, 16 Dec 2022 15:42:16 +0800 Subject: [PATCH 1/7] Create csharp project to replace single file. --- codes/csharp/.gitignore | 5 +++++ codes/csharp/hello-algo.csproj | 18 ++++++++++++++++++ codes/csharp/hello-algo.sln | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 codes/csharp/.gitignore create mode 100644 codes/csharp/hello-algo.csproj create mode 100644 codes/csharp/hello-algo.sln diff --git a/codes/csharp/.gitignore b/codes/csharp/.gitignore new file mode 100644 index 000000000..a4b66a94a --- /dev/null +++ b/codes/csharp/.gitignore @@ -0,0 +1,5 @@ +.idea/ +.vs/ +obj/ +.Debug +bin/ diff --git a/codes/csharp/hello-algo.csproj b/codes/csharp/hello-algo.csproj new file mode 100644 index 000000000..2ef4da851 --- /dev/null +++ b/codes/csharp/hello-algo.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + hello_algo + enable + enable + + + + + + + + + + diff --git a/codes/csharp/hello-algo.sln b/codes/csharp/hello-algo.sln new file mode 100644 index 000000000..df62e5480 --- /dev/null +++ b/codes/csharp/hello-algo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32421.90 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hello-algo", "hello-algo.csproj", "{C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {93C0511D-8D01-43BB-BD42-8E053E46FD67} + EndGlobalSection +EndGlobal From 5c999939f6234b193085ca0a8ab1e1364343fe1a Mon Sep 17 00:00:00 2001 From: ming <1195669834@qq.com> Date: Fri, 16 Dec 2022 15:49:09 +0800 Subject: [PATCH 2/7] Create csharp LinkedList and its unit test. --- .../LinkedList.cs | 64 +++++++++++++++ codes/csharp/include/ListNode.cs | 68 ++++++++++++++++ .../LinkedListTest.cs | 80 +++++++++++++++++++ 3 files changed, 212 insertions(+) create mode 100644 codes/csharp/chapter_array_and_linkedlist/LinkedList.cs create mode 100644 codes/csharp/include/ListNode.cs create mode 100644 codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs diff --git a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs new file mode 100644 index 000000000..27f3bb3df --- /dev/null +++ b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs @@ -0,0 +1,64 @@ +// File: LinkedList.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + +using hello_algo.include; + +namespace hello_algo.chapter_array_and_linkedlist +{ + public class LinkedList + { + /// + ///在链表的结点 n0 之后插入结点 P + /// + public static void Insert(ListNode n0, ListNode P) + { + ListNode n1 = n0.next; + n0.next = P; + P.next = n1; + } + + /// + /// 删除链表的结点 n0 之后的首个结点 + /// + public static void Remove(ListNode n0) + { + if (n0.next == null) + return; + // n0 -> P -> n1 + ListNode P = n0.next; + ListNode n1 = P.next; + n0.next = n1; + } + + /// + ///访问链表中索引为 index 的结点 + /// + public static ListNode Access(ListNode head, int index) + { + for (int i = 0; i < index; i++) + { + head = head.next; + if (head == null) + return null; + } + return head; + } + + /// + /// 在链表中查找值为 target 的首个结点 + /// + public static int Find(ListNode head, int target) + { + int index = 0; + while (head != null) + { + if (head.val == target) + return index; + head = head.next; + index++; + } + return -1; + } + } +} \ No newline at end of file diff --git a/codes/csharp/include/ListNode.cs b/codes/csharp/include/ListNode.cs new file mode 100644 index 000000000..580a1d376 --- /dev/null +++ b/codes/csharp/include/ListNode.cs @@ -0,0 +1,68 @@ +// File: ListNode.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + +namespace hello_algo.include +{ + /// + /// Definition for a singly-linked list node + /// + public class ListNode + { + public int val; + public ListNode next; + + /// + /// Generate a linked list with an array + /// + /// + public ListNode(int x) + { + val = x; + } + + /// + /// Generate a linked list with an array + /// + /// + /// + public static ListNode ArrToLinkedList(int[] arr) + { + ListNode dum = new ListNode(0); + ListNode head = dum; + foreach (int val in arr) + { + head.next = new ListNode(val); + head = head.next; + } + return dum.next; + } + + /// + /// Get a list node with specific value from a linked list + /// + /// + /// + /// + public static ListNode GetListNode(ListNode head, int val) + { + while (head != null && head.val != val) + { + head = head.next; + } + return head; + } + + public override string? ToString() + { + List list = new(); + var head = this; + while (head != null) + { + list.Add(head.val.ToString()); + head = head.next; + } + return string.Join("->", list); + } + } +} \ No newline at end of file diff --git a/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs new file mode 100644 index 000000000..dcaac5356 --- /dev/null +++ b/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs @@ -0,0 +1,80 @@ +// File: LinkedListTest.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + +using hello_algo.chapter_array_and_linkedlist; +using hello_algo.include; +using NUnit.Framework; + +namespace hello_algo.Test.chapter_array_and_linkedlist +{ + [TestFixture] + internal class LinkedListTest + { + private ListNode n0; + private ListNode n1; + private ListNode n2; + private ListNode n3; + private ListNode n4; + + [SetUp] + public void SetUp() + { + // 初始化各结点 + n0 = new ListNode(1); + n1 = new ListNode(3); + n2 = new ListNode(2); + n3 = new ListNode(5); + n4 = new ListNode(4); + // 构建引用指向 + n0.next = n1; + n1.next = n2; + n2.next = n3; + n3.next = n4; + } + + [Test] + public void CheckInit() + { + //检查初始化是否正确 + Console.WriteLine($"初始化的链表为{n0}"); + Assert.AreEqual(n0.ToString(), "1->3->2->5->4"); + } + + [Test] + public void TestInsert() + { + //插入结点 + LinkedList.Insert(n0, new ListNode(0)); + Console.WriteLine($"插入结点后的链表为{n0}"); + Assert.AreEqual(n0.ToString(), "1->0->3->2->5->4"); + } + + [Test] + public void TestRemove() + { + //删除结点 + LinkedList.Remove(n0); + Console.WriteLine($"删除节点后的链表为{n0}"); + Assert.AreEqual(n0.ToString(), "1->2->5->4"); + } + + [Test] + public void TestAccess() + { + //访问结点 + var node = LinkedList.Access(n0, 3); + Console.WriteLine($"链表中索引 3 处的结点的值 ={node.val}"); + Assert.AreEqual(node.val, 5); + } + + [Test] + public void TestFind() + { + //查找结点 + int index = LinkedList.Find(n0, 2); + Console.WriteLine($"链表中值为 2 的结点的索引 = {index}"); + Assert.AreEqual(index, 2); + } + } +} \ No newline at end of file From aa868cfa3d22e432410b9858fb1afc2450ca50fb Mon Sep 17 00:00:00 2001 From: ming <1195669834@qq.com> Date: Fri, 16 Dec 2022 15:53:26 +0800 Subject: [PATCH 3/7] Update csharp array to unify code style,and create its unit test. --- .../chapter_array_and_linkedlist/Array.cs | 59 ++++------------ .../chapter_array_and_linkedlist/ArrayTest.cs | 70 +++++++++++++++++++ 2 files changed, 82 insertions(+), 47 deletions(-) create mode 100644 codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs diff --git a/codes/csharp/chapter_array_and_linkedlist/Array.cs b/codes/csharp/chapter_array_and_linkedlist/Array.cs index 8f836a4f1..e454ff696 100644 --- a/codes/csharp/chapter_array_and_linkedlist/Array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/Array.cs @@ -1,14 +1,12 @@ -/* -* File: Array.cs -* Created Time: 2022-12-14 -* Author: mingXta (1195669834@qq.com) -*/ - +// File: Array.cs +// Created Time: 2022-12-14 +// Author: mingXta (1195669834@qq.com) + namespace hello_algo.chapter_array_and_linkedlist { public class Array { - /* 随机返回一个数组元素 */ + //随机返回一个数组元素 public static int RandomAccess(int[] nums) { Random random = new(); @@ -17,7 +15,7 @@ namespace hello_algo.chapter_array_and_linkedlist return randomNum; } - /* 扩展数组长度 */ + //扩展数组长度 public static int[] Extend(int[] nums, int enlarge) { // 初始化一个扩展长度后的数组 @@ -31,7 +29,7 @@ namespace hello_algo.chapter_array_and_linkedlist return res; } - /* 在数组的索引 index 处插入元素 num */ + //在数组的索引 index 处插入元素 num public static void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 @@ -43,7 +41,7 @@ namespace hello_algo.chapter_array_and_linkedlist nums[index] = num; } - /* 删除索引 index 处元素 */ + //删除索引 index 处元素 public static void Remove(int[] nums, int index) { // 把索引 index 之后的所有元素向前移动一位 @@ -53,7 +51,7 @@ namespace hello_algo.chapter_array_and_linkedlist } } - /* 遍历数组 */ + //遍历数组 public static void Traverse(int[] nums) { int count = 0; @@ -69,7 +67,7 @@ namespace hello_algo.chapter_array_and_linkedlist } } - /* 在数组中查找指定元素 */ + //在数组中查找指定元素 public static int Find(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) @@ -80,43 +78,10 @@ namespace hello_algo.chapter_array_and_linkedlist return -1; } - /*辅助函数,数组转字符串 */ + //辅助函数,数组转字符串 public static string ToString(int[] nums) { return string.Join(",", nums); } - - /* Driver Code */ - public static void Main() - { - /* 初始化数组 */ - int[] arr = new int[5]; - Console.WriteLine("数组 arr = " + ToString(arr)); - int[] nums = { 1, 3, 2, 5, 4 }; - Console.WriteLine("数组 nums = " + ToString(nums)); - - /* 随机访问 */ - int randomNum = RandomAccess(nums); - Console.WriteLine("在 nums 中获取随机元素 " + randomNum); - - /* 长度扩展 */ - nums = Extend(nums, 3); - Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums)); - - /* 插入元素 */ - Insert(nums, 6, 3); - Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums)); - - /* 删除元素 */ - Remove(nums, 2); - Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums)); - - /* 遍历数组 */ - Traverse(nums); - - /* 查找元素 */ - int index = Find(nums, 3); - Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index); - } } -} +} \ No newline at end of file diff --git a/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs new file mode 100644 index 000000000..8bda89513 --- /dev/null +++ b/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs @@ -0,0 +1,70 @@ +// File: ArrayTest.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + +using NUnit.Framework; +using Array = hello_algo.chapter_array_and_linkedlist.Array; + +namespace hello_algo.Test.chapter_array_and_linkedlist +{ + [TestFixture] + internal class ArrayTest + { + private int[] nums; + + [SetUp] + public void setup() + { + //初始化数组 + nums = new int[] { 1, 3, 2, 5, 4 }; + } + + [Test] + public void TestRandomAccess() + { + //随机访问 + int randomNum = Array.RandomAccess(nums); + Console.WriteLine($"在 nums 中获取随机元素 {randomNum}"); + Assert.Contains(randomNum, nums); + } + + [Test] + public void TestExtend() + { + //长度扩展 + int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 }; + nums = Array.Extend(nums, 3); + Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}"); + Assert.AreEqual(target, nums); + } + + [Test] + public void TestInsert() + { + //插入元素 + int[] target = { 1, 3, 2, 6, 5 }; + Array.Insert(nums, 6, 3); + Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}"); + Assert.AreEqual(target, nums); + } + + [Test] + public void TestRemove() + { + //删除元素 + int[] target = { 1, 3, 5, 4, 4 }; + Array.Remove(nums, 2); + Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}"); + Assert.AreEqual(target, nums); + } + + [Test] + public void TestFind() + { + //查找元素 + int index = Array.Find(nums, 3); + Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index); + Assert.AreEqual(1, index); + } + } +} \ No newline at end of file From 0a0374efa00422d19facac353ccaf4c70e7a79b2 Mon Sep 17 00:00:00 2001 From: ming <1195669834@qq.com> Date: Fri, 16 Dec 2022 16:28:19 +0800 Subject: [PATCH 4/7] Update linked_list.md to add csharp code. --- .../linked_list.md | 72 +++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 7ebdbd7e0..6ad1d5658 100644 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -91,7 +91,13 @@ comments: true === "C#" ```csharp title="" - + // 链表结点类 + class ListNode + { + int val; // 结点值 + ListNode next; // 指向下一结点的引用 + ListNode(int x) => val = x; //构造函数 + } ``` **尾结点指向什么?** 我们一般将链表的最后一个结点称为「尾结点」,其指向的是「空」,在 Java / C++ / Python 中分别记为 `null` / `nullptr` / `None` 。在不引起歧义下,本书都使用 `null` 来表示空。 @@ -202,7 +208,18 @@ comments: true === "C#" ```csharp title="" - + // 初始化链表 1 -> 3 -> 2 -> 5 -> 4 + // 初始化各结点 + n0 = new ListNode(1); + n1 = new ListNode(3); + n2 = new ListNode(2); + n3 = new ListNode(5); + n4 = new ListNode(4); + // 构建引用指向 + n0.next = n1; + n1.next = n2; + n2.next = n3; + n3.next = n4; ``` ## 链表优点 @@ -331,7 +348,24 @@ comments: true === "C#" ```csharp title="" + // 在链表的结点 n0 之后插入结点 P + void Insert(ListNode n0, ListNode P) + { + ListNode n1 = n0.next; + n0.next = P; + P.next = n1; + } + // 删除链表的结点 n0 之后的首个结点 + void Remove(ListNode n0) + { + if (n0.next == null) + return; + // n0 -> P -> n1 + ListNode P = n0.next; + ListNode n1 = P.next; + n0.next = n1; + } ``` ## 链表缺点 @@ -422,7 +456,17 @@ comments: true === "C#" ```csharp title="" - + // 访问链表中索引为 index 的结点 + ListNode Access(ListNode head, int index) + { + for (int i = 0; i < index; i++) + { + head = head.next; + if (head == null) + return null; + } + return head; + } ``` **链表的内存占用多。** 链表以结点为单位,每个结点除了保存值外,还需额外保存指针(引用)。这意味着同样数据量下,链表比数组需要占用更多内存空间。 @@ -526,7 +570,19 @@ comments: true === "C#" ```csharp title="" - + // 在链表中查找值为 target 的首个结点 + int Find(ListNode head, int target) + { + int index = 0; + while (head != null) + { + if (head.val == target) + return index; + head = head.next; + index++; + } + return -1; + } ``` ## 常见链表类型 @@ -619,7 +675,13 @@ comments: true === "C#" ```csharp title="" - + // 双向链表结点类 + class ListNode { + int val; // 结点值 + ListNode next; // 指向后继结点的指针(引用) + ListNode prev; // 指向前驱结点的指针(引用) + ListNode(int x) => val = x; // 构造函数 + } ``` ![linkedlist_common_types](linked_list.assets/linkedlist_common_types.png) From 49756d8c7e9e6dac8891368fe08026455b30d635 Mon Sep 17 00:00:00 2001 From: ming <1195669834@qq.com> Date: Fri, 16 Dec 2022 16:57:16 +0800 Subject: [PATCH 5/7] Modify some formats. --- .../chapter_array_and_linkedlist/Array.cs | 28 ++++++++++++++----- .../LinkedList.cs | 4 +-- .../chapter_array_and_linkedlist/ArrayTest.cs | 14 +++++----- .../LinkedListTest.cs | 12 ++++---- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/codes/csharp/chapter_array_and_linkedlist/Array.cs b/codes/csharp/chapter_array_and_linkedlist/Array.cs index e454ff696..a0c74c533 100644 --- a/codes/csharp/chapter_array_and_linkedlist/Array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/Array.cs @@ -6,7 +6,9 @@ namespace hello_algo.chapter_array_and_linkedlist { public class Array { - //随机返回一个数组元素 + /// + /// 随机返回一个数组元素 + /// public static int RandomAccess(int[] nums) { Random random = new(); @@ -15,7 +17,9 @@ namespace hello_algo.chapter_array_and_linkedlist return randomNum; } - //扩展数组长度 + /// + /// 扩展数组长度 + /// public static int[] Extend(int[] nums, int enlarge) { // 初始化一个扩展长度后的数组 @@ -29,7 +33,9 @@ namespace hello_algo.chapter_array_and_linkedlist return res; } - //在数组的索引 index 处插入元素 num + /// + /// 在数组的索引 index 处插入元素 num + /// public static void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 @@ -41,7 +47,9 @@ namespace hello_algo.chapter_array_and_linkedlist nums[index] = num; } - //删除索引 index 处元素 + /// + /// 删除索引 index 处元素 + /// public static void Remove(int[] nums, int index) { // 把索引 index 之后的所有元素向前移动一位 @@ -51,7 +59,9 @@ namespace hello_algo.chapter_array_and_linkedlist } } - //遍历数组 + /// + /// 遍历数组 + /// public static void Traverse(int[] nums) { int count = 0; @@ -67,7 +77,9 @@ namespace hello_algo.chapter_array_and_linkedlist } } - //在数组中查找指定元素 + /// + /// 在数组中查找指定元素 + /// public static int Find(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) @@ -78,7 +90,9 @@ namespace hello_algo.chapter_array_and_linkedlist return -1; } - //辅助函数,数组转字符串 + /// + /// 辅助函数,数组转字符串 + /// public static string ToString(int[] nums) { return string.Join(",", nums); diff --git a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs index 27f3bb3df..e09f38e27 100644 --- a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs +++ b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs @@ -9,7 +9,7 @@ namespace hello_algo.chapter_array_and_linkedlist public class LinkedList { /// - ///在链表的结点 n0 之后插入结点 P + /// 在链表的结点 n0 之后插入结点 P /// public static void Insert(ListNode n0, ListNode P) { @@ -32,7 +32,7 @@ namespace hello_algo.chapter_array_and_linkedlist } /// - ///访问链表中索引为 index 的结点 + /// 访问链表中索引为 index 的结点 /// public static ListNode Access(ListNode head, int index) { diff --git a/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs index 8bda89513..02d89d6ad 100644 --- a/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs +++ b/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs @@ -5,7 +5,7 @@ using NUnit.Framework; using Array = hello_algo.chapter_array_and_linkedlist.Array; -namespace hello_algo.Test.chapter_array_and_linkedlist +namespace hello_algo.test.chapter_array_and_linkedlist { [TestFixture] internal class ArrayTest @@ -15,14 +15,14 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [SetUp] public void setup() { - //初始化数组 + // 初始化数组 nums = new int[] { 1, 3, 2, 5, 4 }; } [Test] public void TestRandomAccess() { - //随机访问 + // 随机访问 int randomNum = Array.RandomAccess(nums); Console.WriteLine($"在 nums 中获取随机元素 {randomNum}"); Assert.Contains(randomNum, nums); @@ -31,7 +31,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestExtend() { - //长度扩展 + // 长度扩展 int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 }; nums = Array.Extend(nums, 3); Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}"); @@ -41,7 +41,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestInsert() { - //插入元素 + // 插入元素 int[] target = { 1, 3, 2, 6, 5 }; Array.Insert(nums, 6, 3); Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}"); @@ -51,7 +51,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestRemove() { - //删除元素 + // 删除元素 int[] target = { 1, 3, 5, 4, 4 }; Array.Remove(nums, 2); Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}"); @@ -61,7 +61,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestFind() { - //查找元素 + // 查找元素 int index = Array.Find(nums, 3); Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index); Assert.AreEqual(1, index); diff --git a/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs index dcaac5356..e98007218 100644 --- a/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs +++ b/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs @@ -6,7 +6,7 @@ using hello_algo.chapter_array_and_linkedlist; using hello_algo.include; using NUnit.Framework; -namespace hello_algo.Test.chapter_array_and_linkedlist +namespace hello_algo.test.chapter_array_and_linkedlist { [TestFixture] internal class LinkedListTest @@ -36,7 +36,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void CheckInit() { - //检查初始化是否正确 + // 检查初始化是否正确 Console.WriteLine($"初始化的链表为{n0}"); Assert.AreEqual(n0.ToString(), "1->3->2->5->4"); } @@ -44,7 +44,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestInsert() { - //插入结点 + // 插入结点 LinkedList.Insert(n0, new ListNode(0)); Console.WriteLine($"插入结点后的链表为{n0}"); Assert.AreEqual(n0.ToString(), "1->0->3->2->5->4"); @@ -53,7 +53,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestRemove() { - //删除结点 + // 删除结点 LinkedList.Remove(n0); Console.WriteLine($"删除节点后的链表为{n0}"); Assert.AreEqual(n0.ToString(), "1->2->5->4"); @@ -62,7 +62,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestAccess() { - //访问结点 + // 访问结点 var node = LinkedList.Access(n0, 3); Console.WriteLine($"链表中索引 3 处的结点的值 ={node.val}"); Assert.AreEqual(node.val, 5); @@ -71,7 +71,7 @@ namespace hello_algo.Test.chapter_array_and_linkedlist [Test] public void TestFind() { - //查找结点 + // 查找结点 int index = LinkedList.Find(n0, 2); Console.WriteLine($"链表中值为 2 的结点的索引 = {index}"); Assert.AreEqual(index, 2); From f08621241f6981a4c93a4abaccb778b45c386d3e Mon Sep 17 00:00:00 2001 From: Ming <1195669834@qq.com> Date: Sat, 17 Dec 2022 22:11:25 +0800 Subject: [PATCH 6/7] Update the project to make it simpler. --- .../chapter_array_and_linkedlist/Array.cs | 36 +++++++++ .../LinkedList.cs | 36 +++++++++ codes/csharp/hello-algo.sln | 25 ------ .../chapter_array_and_linkedlist/ArrayTest.cs | 70 ---------------- .../LinkedListTest.cs | 80 ------------------- 5 files changed, 72 insertions(+), 175 deletions(-) delete mode 100644 codes/csharp/hello-algo.sln delete mode 100644 codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs delete mode 100644 codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs diff --git a/codes/csharp/chapter_array_and_linkedlist/Array.cs b/codes/csharp/chapter_array_and_linkedlist/Array.cs index a0c74c533..72584c0af 100644 --- a/codes/csharp/chapter_array_and_linkedlist/Array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/Array.cs @@ -2,6 +2,8 @@ // Created Time: 2022-12-14 // Author: mingXta (1195669834@qq.com) +using NUnit.Framework; + namespace hello_algo.chapter_array_and_linkedlist { public class Array @@ -97,5 +99,39 @@ namespace hello_algo.chapter_array_and_linkedlist { return string.Join(",", nums); } + + // Driver Code + [Test] + public static void Test() + { + // 初始化数组 + int[] arr = new int[5]; + Console.WriteLine("数组 arr = " + ToString(arr)); + int[] nums = { 1, 3, 2, 5, 4 }; + Console.WriteLine("数组 nums = " + ToString(nums)); + + // 随机访问 + int randomNum = RandomAccess(nums); + Console.WriteLine("在 nums 中获取随机元素 " + randomNum); + + // 长度扩展 + nums = Extend(nums, 3); + Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums)); + + // 插入元素 + Insert(nums, 6, 3); + Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums)); + + // 删除元素 + Remove(nums, 2); + Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums)); + + // 遍历数组 + Traverse(nums); + + // 查找元素 + int index = Find(nums, 3); + Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index); + } } } \ No newline at end of file diff --git a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs index e09f38e27..46b87a624 100644 --- a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs +++ b/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs @@ -3,6 +3,7 @@ // Author: mingXta (1195669834@qq.com) using hello_algo.include; +using NUnit.Framework; namespace hello_algo.chapter_array_and_linkedlist { @@ -60,5 +61,40 @@ namespace hello_algo.chapter_array_and_linkedlist } return -1; } + + // Driver Code + [Test] + public void Test() + { + // 初始化链表 + // 初始化各个结点 + ListNode n0 = new ListNode(1); + ListNode n1 = new ListNode(3); + ListNode n2 = new ListNode(2); + ListNode n3 = new ListNode(5); + ListNode n4 = new ListNode(4); + // 构建引用指向 + n0.next = n1; + n1.next = n2; + n2.next = n3; + n3.next = n4; + Console.WriteLine($"初始化的链表为{n0}"); + + // 插入结点 + Insert(n0, new ListNode(0)); + Console.WriteLine($"插入结点后的链表为{n0}"); + + // 删除结点 + Remove(n0); + Console.WriteLine($"删除结点后的链表为{n0}"); + + // 访问结点 + ListNode node = Access(n0, 3); + Console.WriteLine($"链表中索引 3 处的结点的值 = {node.val}"); + + // 查找结点 + int index = Find(n0, 2); + Console.WriteLine($"链表中值为 2 的结点的索引 = {index}"); + } } } \ No newline at end of file diff --git a/codes/csharp/hello-algo.sln b/codes/csharp/hello-algo.sln deleted file mode 100644 index df62e5480..000000000 --- a/codes/csharp/hello-algo.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.1.32421.90 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hello-algo", "hello-algo.csproj", "{C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C88820BB-BD9C-4993-9FAE-D3C09FEF4E4B}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {93C0511D-8D01-43BB-BD42-8E053E46FD67} - EndGlobalSection -EndGlobal diff --git a/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs deleted file mode 100644 index 02d89d6ad..000000000 --- a/codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs +++ /dev/null @@ -1,70 +0,0 @@ -// File: ArrayTest.cs -// Created Time: 2022-12-16 -// Author: mingXta (1195669834@qq.com) - -using NUnit.Framework; -using Array = hello_algo.chapter_array_and_linkedlist.Array; - -namespace hello_algo.test.chapter_array_and_linkedlist -{ - [TestFixture] - internal class ArrayTest - { - private int[] nums; - - [SetUp] - public void setup() - { - // 初始化数组 - nums = new int[] { 1, 3, 2, 5, 4 }; - } - - [Test] - public void TestRandomAccess() - { - // 随机访问 - int randomNum = Array.RandomAccess(nums); - Console.WriteLine($"在 nums 中获取随机元素 {randomNum}"); - Assert.Contains(randomNum, nums); - } - - [Test] - public void TestExtend() - { - // 长度扩展 - int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 }; - nums = Array.Extend(nums, 3); - Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}"); - Assert.AreEqual(target, nums); - } - - [Test] - public void TestInsert() - { - // 插入元素 - int[] target = { 1, 3, 2, 6, 5 }; - Array.Insert(nums, 6, 3); - Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}"); - Assert.AreEqual(target, nums); - } - - [Test] - public void TestRemove() - { - // 删除元素 - int[] target = { 1, 3, 5, 4, 4 }; - Array.Remove(nums, 2); - Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}"); - Assert.AreEqual(target, nums); - } - - [Test] - public void TestFind() - { - // 查找元素 - int index = Array.Find(nums, 3); - Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index); - Assert.AreEqual(1, index); - } - } -} \ No newline at end of file diff --git a/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs b/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs deleted file mode 100644 index e98007218..000000000 --- a/codes/csharp/test/chapter_array_and_linkedlist/LinkedListTest.cs +++ /dev/null @@ -1,80 +0,0 @@ -// File: LinkedListTest.cs -// Created Time: 2022-12-16 -// Author: mingXta (1195669834@qq.com) - -using hello_algo.chapter_array_and_linkedlist; -using hello_algo.include; -using NUnit.Framework; - -namespace hello_algo.test.chapter_array_and_linkedlist -{ - [TestFixture] - internal class LinkedListTest - { - private ListNode n0; - private ListNode n1; - private ListNode n2; - private ListNode n3; - private ListNode n4; - - [SetUp] - public void SetUp() - { - // 初始化各结点 - n0 = new ListNode(1); - n1 = new ListNode(3); - n2 = new ListNode(2); - n3 = new ListNode(5); - n4 = new ListNode(4); - // 构建引用指向 - n0.next = n1; - n1.next = n2; - n2.next = n3; - n3.next = n4; - } - - [Test] - public void CheckInit() - { - // 检查初始化是否正确 - Console.WriteLine($"初始化的链表为{n0}"); - Assert.AreEqual(n0.ToString(), "1->3->2->5->4"); - } - - [Test] - public void TestInsert() - { - // 插入结点 - LinkedList.Insert(n0, new ListNode(0)); - Console.WriteLine($"插入结点后的链表为{n0}"); - Assert.AreEqual(n0.ToString(), "1->0->3->2->5->4"); - } - - [Test] - public void TestRemove() - { - // 删除结点 - LinkedList.Remove(n0); - Console.WriteLine($"删除节点后的链表为{n0}"); - Assert.AreEqual(n0.ToString(), "1->2->5->4"); - } - - [Test] - public void TestAccess() - { - // 访问结点 - var node = LinkedList.Access(n0, 3); - Console.WriteLine($"链表中索引 3 处的结点的值 ={node.val}"); - Assert.AreEqual(node.val, 5); - } - - [Test] - public void TestFind() - { - // 查找结点 - int index = LinkedList.Find(n0, 2); - Console.WriteLine($"链表中值为 2 的结点的索引 = {index}"); - Assert.AreEqual(index, 2); - } - } -} \ No newline at end of file From 5ce041e245bf52463a2581c86502f84ffb5ecc6e Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sun, 18 Dec 2022 02:34:52 +0800 Subject: [PATCH 7/7] Update c# installation guidance. --- docs/chapter_preface/installation.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/chapter_preface/installation.md b/docs/chapter_preface/installation.md index 05919c3f4..420412559 100644 --- a/docs/chapter_preface/installation.md +++ b/docs/chapter_preface/installation.md @@ -12,7 +12,7 @@ comments: true ## Java 环境 -1. 下载并安装 [OpenJDK](https://jdk.java.net/18/) ,获取 Java 运行环境。 +1. 下载并安装 [OpenJDK](https://jdk.java.net/18/) 。 2. 在 VSCode 的插件市场中搜索 `java` ,安装 Java Extension Pack 。 ## C++ 环境 @@ -22,16 +22,21 @@ comments: true ## Python 环境 -1. 下载并安装 [Miniconda3](https://docs.conda.io/en/latest/miniconda.html) ,获取 Python 运行环境。 +1. 下载并安装 [Miniconda3](https://docs.conda.io/en/latest/miniconda.html) 。 2. 在 VSCode 的插件市场中搜索 `python` ,安装 Python Extension Pack 。 ## Go 环境 -1. 下载并安装 [go](https://go.dev/dl/) ,获取 Go 运行环境。 +1. 下载并安装 [go](https://go.dev/dl/) 。 2. 在 VSCode 的插件市场中搜索 `go` ,安装 Go 。 3. 快捷键 `Ctrl + Shift + P` 呼出命令栏,输入 go ,选择 `Go: Install/Update Tools` ,全部勾选并安装即可。 ## JavaScript 环境 -1. 下载并安装 [node.js](https://nodejs.org/en/) ,获取 JavaScript 运行环境。 +1. 下载并安装 [node.js](https://nodejs.org/en/) 。 2. 在 VSCode 的插件市场中搜索 `javascript` ,安装 JavaScript (ES6) code snippets 。 + +## C# 环境 + +1. 下载并安装 [.Net 6.0](https://dotnet.microsoft.com/en-us/download) ; +2. 在 VSCode 的插件市场中搜索 `c#` ,安装 c# 。