mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:36:30 +08:00
Create csharp LinkedList and its unit test.
This commit is contained in:
parent
47bf6729a4
commit
5c999939f6
3 changed files with 212 additions and 0 deletions
64
codes/csharp/chapter_array_and_linkedlist/LinkedList.cs
Normal file
64
codes/csharp/chapter_array_and_linkedlist/LinkedList.cs
Normal file
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
///在链表的结点 n0 之后插入结点 P
|
||||
/// </summary>
|
||||
public static void Insert(ListNode n0, ListNode P)
|
||||
{
|
||||
ListNode n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除链表的结点 n0 之后的首个结点
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///访问链表中索引为 index 的结点
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在链表中查找值为 target 的首个结点
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
68
codes/csharp/include/ListNode.cs
Normal file
68
codes/csharp/include/ListNode.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
// File: ListNode.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.include
|
||||
{
|
||||
/// <summary>
|
||||
/// Definition for a singly-linked list node
|
||||
/// </summary>
|
||||
public class ListNode
|
||||
{
|
||||
public int val;
|
||||
public ListNode next;
|
||||
|
||||
/// <summary>
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
public ListNode(int x)
|
||||
{
|
||||
val = x;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a linked list with an array
|
||||
/// </summary>
|
||||
/// <param name="arr"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list node with specific value from a linked list
|
||||
/// </summary>
|
||||
/// <param name="head"></param>
|
||||
/// <param name="val"></param>
|
||||
/// <returns></returns>
|
||||
public static ListNode GetListNode(ListNode head, int val)
|
||||
{
|
||||
while (head != null && head.val != val)
|
||||
{
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
public override string? ToString()
|
||||
{
|
||||
List<string> list = new();
|
||||
var head = this;
|
||||
while (head != null)
|
||||
{
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
}
|
||||
return string.Join("->", list);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue