From c429e5f0bb583eac68a939e549ffeed8c6d2f1f2 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 23 Dec 2022 17:10:40 +0800 Subject: [PATCH] 1. Remove unused libs. 2. Add file headers. 3. Modify file name to match Java's. 4. Fix some issues. --- .../chapter_array_and_linkedlist/Array.cs | 4 +-- .../{LinkedList.cs => linked_list.cs} | 14 +++++----- .../chapter_array_and_linkedlist/list.cs | 13 ++++------ .../chapter_array_and_linkedlist/my_list.cs | 10 +++---- codes/csharp/include/ListNode.cs | 6 ++--- codes/csharp/include/PrintUtil.cs | 26 +++---------------- codes/csharp/include/TreeNode.cs | 6 ++++- 7 files changed, 30 insertions(+), 49 deletions(-) rename codes/csharp/chapter_array_and_linkedlist/{LinkedList.cs => linked_list.cs} (88%) diff --git a/codes/csharp/chapter_array_and_linkedlist/Array.cs b/codes/csharp/chapter_array_and_linkedlist/Array.cs index cc6b79dd..31be31da 100644 --- a/codes/csharp/chapter_array_and_linkedlist/Array.cs +++ b/codes/csharp/chapter_array_and_linkedlist/Array.cs @@ -1,4 +1,4 @@ -// File: Array.cs +// File: array.cs // Created Time: 2022-12-14 // Author: mingXta (1195669834@qq.com) @@ -6,7 +6,7 @@ using NUnit.Framework; namespace hello_algo.chapter_array_and_linkedlist { - public class Array + public class array { /// /// 随机返回一个数组元素 diff --git a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs similarity index 88% rename from codes/csharp/chapter_array_and_linkedlist/LinkedList.cs rename to codes/csharp/chapter_array_and_linkedlist/linked_list.cs index ecc7aabc..16bb9d64 100644 --- a/codes/csharp/chapter_array_and_linkedlist/LinkedList.cs +++ b/codes/csharp/chapter_array_and_linkedlist/linked_list.cs @@ -1,4 +1,4 @@ -// File: LinkedList.cs +// File: linked_list.cs // Created Time: 2022-12-16 // Author: mingXta (1195669834@qq.com) @@ -7,14 +7,14 @@ using NUnit.Framework; namespace hello_algo.chapter_array_and_linkedlist { - public class LinkedList + public class linked_list { /// /// 在链表的结点 n0 之后插入结点 P /// public static void Insert(ListNode n0, ListNode P) { - ListNode n1 = n0.next; + ListNode? n1 = n0.next; n0.next = P; P.next = n1; } @@ -28,7 +28,7 @@ namespace hello_algo.chapter_array_and_linkedlist return; // n0 -> P -> n1 ListNode P = n0.next; - ListNode n1 = P.next; + ListNode? n1 = P.next; n0.next = n1; } @@ -78,15 +78,15 @@ namespace hello_algo.chapter_array_and_linkedlist n1.next = n2; n2.next = n3; n3.next = n4; - Console.WriteLine($"初始化的链表为{n0}"); + Console.WriteLine($"初始化的链表为 {n0}"); // 插入结点 Insert(n0, new ListNode(0)); - Console.WriteLine($"插入结点后的链表为{n0}"); + Console.WriteLine($"插入结点后的链表为 {n0}"); // 删除结点 Remove(n0); - Console.WriteLine($"删除结点后的链表为{n0}"); + Console.WriteLine($"删除结点后的链表为 {n0}"); // 访问结点 ListNode? node = Access(n0, 3); diff --git a/codes/csharp/chapter_array_and_linkedlist/list.cs b/codes/csharp/chapter_array_and_linkedlist/list.cs index 9530f24a..9745d064 100644 --- a/codes/csharp/chapter_array_and_linkedlist/list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/list.cs @@ -1,11 +1,8 @@ -using Newtonsoft.Json.Linq; +// File: list.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml.Linq; namespace hello_algo.chapter_array_and_linkedlist { @@ -46,7 +43,7 @@ namespace hello_algo.chapter_array_and_linkedlist Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list)); /* 删除元素 */ - list.Remove(3); + list.RemoveAt(3); Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list)); /* 通过索引遍历列表 */ diff --git a/codes/csharp/chapter_array_and_linkedlist/my_list.cs b/codes/csharp/chapter_array_and_linkedlist/my_list.cs index 8ccf3595..6a6568d4 100644 --- a/codes/csharp/chapter_array_and_linkedlist/my_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/my_list.cs @@ -1,10 +1,8 @@ -using Newtonsoft.Json.Linq; +// File: my_list.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace hello_algo.chapter_array_and_linkedlist { diff --git a/codes/csharp/include/ListNode.cs b/codes/csharp/include/ListNode.cs index 580a1d37..0fe2913e 100644 --- a/codes/csharp/include/ListNode.cs +++ b/codes/csharp/include/ListNode.cs @@ -10,7 +10,7 @@ namespace hello_algo.include public class ListNode { public int val; - public ListNode next; + public ListNode? next; /// /// Generate a linked list with an array @@ -26,7 +26,7 @@ namespace hello_algo.include /// /// /// - public static ListNode ArrToLinkedList(int[] arr) + public static ListNode? ArrToLinkedList(int[] arr) { ListNode dum = new ListNode(0); ListNode head = dum; @@ -44,7 +44,7 @@ namespace hello_algo.include /// /// /// - public static ListNode GetListNode(ListNode head, int val) + public static ListNode? GetListNode(ListNode? head, int val) { while (head != null && head.val != val) { diff --git a/codes/csharp/include/PrintUtil.cs b/codes/csharp/include/PrintUtil.cs index bdd436ea..86e2bd8d 100644 --- a/codes/csharp/include/PrintUtil.cs +++ b/codes/csharp/include/PrintUtil.cs @@ -1,9 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.SymbolStore; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// File: ListNode.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) namespace hello_algo.include { @@ -21,21 +18,6 @@ namespace hello_algo.include public class PrintUtil { - /** - * Print a linked list - * @param head - */ - public static void printLinkedList(ListNode head) - { - List list = new(); - while (head != null) - { - list.Add(head.val.ToString()); - head = head.next; - } - Console.Write(String.Join(" -> ", list)); - } - /** * The interface of the tree printer * This tree printer is borrowed from TECHIE DELIGHT @@ -81,7 +63,7 @@ namespace hello_algo.include } showTrunks(trunk); - Console.Write(" " + root.val); + Console.WriteLine(" " + root.val); if (prev != null) { diff --git a/codes/csharp/include/TreeNode.cs b/codes/csharp/include/TreeNode.cs index d9130b22..bc4430f2 100644 --- a/codes/csharp/include/TreeNode.cs +++ b/codes/csharp/include/TreeNode.cs @@ -1,9 +1,13 @@ +// File: ListNode.cs +// Created Time: 2022-12-16 +// Author: mingXta (1195669834@qq.com) + namespace hello_algo.include { public class TreeNode { public int? val; // 结点值 - public int height; // 结点高度 + public int height; // 结点高度 public TreeNode? left; // 左子结点引用 public TreeNode? right; // 右子结点引用