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] 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 a0c74c53..72584c0a 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 e09f38e2..46b87a62 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 df62e548..00000000 --- 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 02d89d6a..00000000 --- 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 e9800721..00000000 --- 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