mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Merge pull request #125 from mingXta/linked-list
Add C# linkedlist doc and code,modify some formatting.
This commit is contained in:
commit
9203d47d1d
5
codes/csharp/.gitignore
vendored
Normal file
5
codes/csharp/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.idea/
|
||||
.vs/
|
||||
obj/
|
||||
.Debug
|
||||
bin/
|
@ -1,14 +1,16 @@
|
||||
/*
|
||||
* 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)
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
public class Array
|
||||
{
|
||||
/* 随机返回一个数组元素 */
|
||||
/// <summary>
|
||||
/// 随机返回一个数组元素
|
||||
/// </summary>
|
||||
public static int RandomAccess(int[] nums)
|
||||
{
|
||||
Random random = new();
|
||||
@ -17,7 +19,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
/// <summary>
|
||||
/// 扩展数组长度
|
||||
/// </summary>
|
||||
public static int[] Extend(int[] nums, int enlarge)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
@ -31,7 +35,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
/// <summary>
|
||||
/// 在数组的索引 index 处插入元素 num
|
||||
/// </summary>
|
||||
public static void Insert(int[] nums, int num, int index)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
@ -43,7 +49,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/// <summary>
|
||||
/// 删除索引 index 处元素
|
||||
/// </summary>
|
||||
public static void Remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
@ -53,7 +61,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
/// <summary>
|
||||
/// 遍历数组
|
||||
/// </summary>
|
||||
public static void Traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
@ -69,7 +79,9 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
/// <summary>
|
||||
/// 在数组中查找指定元素
|
||||
/// </summary>
|
||||
public static int Find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
@ -80,43 +92,46 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*辅助函数,数组转字符串 */
|
||||
/// <summary>
|
||||
/// 辅助函数,数组转字符串
|
||||
/// </summary>
|
||||
public static string ToString(int[] nums)
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void Main()
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
100
codes/csharp/chapter_array_and_linkedlist/LinkedList.cs
Normal file
100
codes/csharp/chapter_array_and_linkedlist/LinkedList.cs
Normal file
@ -0,0 +1,100 @@
|
||||
// File: LinkedList.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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}");
|
||||
}
|
||||
}
|
||||
}
|
18
codes/csharp/hello-algo.csproj
Normal file
18
codes/csharp/hello-algo.csproj
Normal file
@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>hello_algo</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user