添加Lesson10

This commit is contained in:
2025-10-15 09:45:51 +08:00
parent 47193206ac
commit 58e0a25def
5 changed files with 160 additions and 0 deletions
+12
View File
@@ -41,6 +41,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson9_顺序存储和链
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson9_顺序存储和链式存储练习", "Lesson9_顺序存储和链式存储练习\Lesson9_顺序存储和链式存储练习.csproj", "{EDA0CEB2-BB75-4149-8C0D-56C9A5286769}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson10_LinkedList", "Lesson10_LinkedList\Lesson10_LinkedList.csproj", "{FB78D018-414D-4F0A-8216-B65E0662A469}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson10_LinkedList练习", "Lesson10_LinkedList练习\Lesson10_LinkedList练习.csproj", "{547A4860-2E6F-4022-802A-F34204F7ADF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -123,6 +127,14 @@ Global
{EDA0CEB2-BB75-4149-8C0D-56C9A5286769}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDA0CEB2-BB75-4149-8C0D-56C9A5286769}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDA0CEB2-BB75-4149-8C0D-56C9A5286769}.Release|Any CPU.Build.0 = Release|Any CPU
{FB78D018-414D-4F0A-8216-B65E0662A469}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB78D018-414D-4F0A-8216-B65E0662A469}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB78D018-414D-4F0A-8216-B65E0662A469}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB78D018-414D-4F0A-8216-B65E0662A469}.Release|Any CPU.Build.0 = Release|Any CPU
{547A4860-2E6F-4022-802A-F34204F7ADF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{547A4860-2E6F-4022-802A-F34204F7ADF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{547A4860-2E6F-4022-802A-F34204F7ADF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{547A4860-2E6F-4022-802A-F34204F7ADF5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+99
View File
@@ -0,0 +1,99 @@
namespace Lesson10_LinkedList
{
internal class Program
{
static void Main(string[] args)
{
#region LinkedList
//LinkedList<T> 是一个双向链表
//是C#为我们封装好的一个数据结构
#endregion
#region
//需要引用命名空间 System.Collections.Generic
//链表对象,需要掌握两个类
//一个是链表本身 LinkedList<T>,一个是链表节点 LinkedListNode<T>
LinkedList<int> linkedList = new LinkedList<int>();
LinkedList<string> linkedList2 = new LinkedList<string>();
#endregion
#region
//增
//1.在链表尾部添加元素
linkedList.AddLast(1);
//2.在链表头部添加元素
linkedList.AddFirst(2);
//3.在某一个节点之后添加一个节点
// 要指定节点,先得到上一个节点
LinkedListNode<int> n = linkedList.Find(1);
linkedList.AddAfter(n, 3);
//4.在某一个节点之前添加一个节点
// 要指定节点,先得到下一个节点
linkedList.AddBefore(n, 4);
//删
//1.移除头节点
linkedList.RemoveFirst();
//2.移除尾节点
linkedList.RemoveLast();
//3.移除指定节点
// 要指定节点,先找到节点
linkedList.Remove(4);//根据值移除节点
//4.清空
linkedList.Clear();
linkedList.AddFirst(1);
linkedList.AddLast(2);
//查
//1.头节点
LinkedListNode<int> first = linkedList.First;
//2.尾节点
LinkedListNode<int> last = linkedList.Last;
//3.查找指定节点
// 无法通过下标获取指定元素
// 只能遍历查找节点
LinkedListNode<int> node = linkedList.Find(1);//根据值查找节点
Console.WriteLine(node.Value);
node = linkedList.FindLast(1);//找不到会返回空
//4.判断是否包含某个值
bool contains = linkedList.Contains(1);
//改
//先得到节点,再修改节点的值
Console.WriteLine(linkedList.First.Value);
linkedList.First.Value = 100;
Console.WriteLine(linkedList.First.Value);
#endregion
#region
//1.使用foreach遍历
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
//2.通过节点遍历
//从头节点开始遍历
LinkedListNode<int> temp = linkedList.First;
while (temp != null)
{
Console.Write(temp.Value + " ");
temp = temp.Next;//指向下一个节点
}
//从尾节点开始遍历
temp = linkedList.Last;
while (temp != null)
{
Console.WriteLine(temp.Value + " ");
temp = temp.Previous;//指向上一个节点
}
#endregion
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,29 @@
namespace Lesson10_LinkedList练习
{
class Program
{
static void Main(string[] args)
{
LinkedList<int> ints = new LinkedList<int>();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
ints.AddLast(r.Next(1,100));
}
LinkedListNode<int> temp = ints.First;
while (temp != null)
{
Console.Write(temp.Value + " ");
temp = temp.Next;
}
Console.WriteLine();
Console.WriteLine("----------------");
temp = ints.Last;
while (temp != null)
{
Console.Write(temp.Value + " ");
temp = temp.Previous;
}
}
}
}