From 47193206ac584773798a17ce136fc13a25e89419 Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Tue, 14 Oct 2025 22:02:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#进阶/C#进阶.sln | 12 ++ .../Lesson9_顺序存储和链式存储.csproj | 10 + C#进阶/Lesson9_顺序存储和链式存储/Program.cs | 154 ++++++++++++++++ .../Lesson9_顺序存储和链式存储练习.csproj | 10 + .../Lesson9_顺序存储和链式存储练习/Program.cs | 174 ++++++++++++++++++ 5 files changed, 360 insertions(+) create mode 100644 C#进阶/Lesson9_顺序存储和链式存储/Lesson9_顺序存储和链式存储.csproj create mode 100644 C#进阶/Lesson9_顺序存储和链式存储/Program.cs create mode 100644 C#进阶/Lesson9_顺序存储和链式存储练习/Lesson9_顺序存储和链式存储练习.csproj create mode 100644 C#进阶/Lesson9_顺序存储和链式存储练习/Program.cs diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 2826e8d..9bbd9cc 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -37,6 +37,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson8_Dictionary", "Lesso EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson8_Dictionary练习", "Lesson8_Dictionary练习\Lesson8_Dictionary练习.csproj", "{1481414C-D336-4691-A45A-6A8CABFD33FE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson9_顺序存储和链式存储", "Lesson9_顺序存储和链式存储\Lesson9_顺序存储和链式存储.csproj", "{317AE884-9270-4AEA-8181-288BC4F1FB2E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson9_顺序存储和链式存储练习", "Lesson9_顺序存储和链式存储练习\Lesson9_顺序存储和链式存储练习.csproj", "{EDA0CEB2-BB75-4149-8C0D-56C9A5286769}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -111,6 +115,14 @@ Global {1481414C-D336-4691-A45A-6A8CABFD33FE}.Debug|Any CPU.Build.0 = Debug|Any CPU {1481414C-D336-4691-A45A-6A8CABFD33FE}.Release|Any CPU.ActiveCfg = Release|Any CPU {1481414C-D336-4691-A45A-6A8CABFD33FE}.Release|Any CPU.Build.0 = Release|Any CPU + {317AE884-9270-4AEA-8181-288BC4F1FB2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {317AE884-9270-4AEA-8181-288BC4F1FB2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {317AE884-9270-4AEA-8181-288BC4F1FB2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {317AE884-9270-4AEA-8181-288BC4F1FB2E}.Release|Any CPU.Build.0 = Release|Any CPU + {EDA0CEB2-BB75-4149-8C0D-56C9A5286769}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#进阶/Lesson9_顺序存储和链式存储/Lesson9_顺序存储和链式存储.csproj b/C#进阶/Lesson9_顺序存储和链式存储/Lesson9_顺序存储和链式存储.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson9_顺序存储和链式存储/Lesson9_顺序存储和链式存储.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson9_顺序存储和链式存储/Program.cs b/C#进阶/Lesson9_顺序存储和链式存储/Program.cs new file mode 100644 index 0000000..db813e6 --- /dev/null +++ b/C#进阶/Lesson9_顺序存储和链式存储/Program.cs @@ -0,0 +1,154 @@ +namespace Lesson9_顺序存储和链式存储 +{ + class Program + { + static void Main(string[] args) + { + #region 知识点一 数据结构 + // 数据结构 + // 数据结构是计算机存储、组织数据的方式(规则) + // 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合 + // 比如自定义的一个类也可以称为一种数据结构 自己定义的数据组合规则 + + // 不要把数据结构想的太复杂 + // 简单点理解,就是人定义的 存储数据 和 表示数据之间关系 的规则而已 + + // 常用的数据结构(前辈总结和制定的一些经典规则) + // 数组、栈、队列、链表、树、图、堆、散列表 + #endregion + + #region 知识点二 线性表 + //线性表是一种数据结构,是由N个具有相同特性的数据元素的有限序列 + //比如数组、Arraylist、stack、queue、链表等 + #endregion + + //顺序存储和链式存储 是数据结构重两种 存储结构 + + #region 知识点三 顺序存储 + //数组、stack、queue、list、arraylist —— 顺序存储 + //只是 数组、stack、queue的 组织规则不同 + //顺序存储: + //用一组地址连续的存储单元一次存储线性表的各个元素 + #endregion + + #region 知识点四 链式存储 + //单向链表、双向链表、循环链表 —— 链式存储 + //链式存储(连接存储): + //用一组任意的存储单元存储线性表重的各个数据元素 + #endregion + + + LinkedList link = new LinkedList(); + link.Add(1); + link.Add(2); + link.Add(3); + link.Add(4); + LinkedNode node = link.head; + while (node != null) + { + Console.WriteLine(node.value); + node = node.nextNode; + } + link.Remove(2); + Console.WriteLine("--------"); + node = link.head; + while (node != null) + { + Console.WriteLine(node.value); + node = node.nextNode; + } + + link.Remove(1); + Console.WriteLine("--------"); + node = link.head; + while (node != null) + { + Console.WriteLine(node.value); + node = node.nextNode; + } + + link.Add(999); + Console.WriteLine("--------"); + node = link.head; + while (node != null) + { + Console.WriteLine(node.value); + node = node.nextNode; + } + } + } + #region 知识点五 自己实现一个最贱的的单向链表 + /// + /// 单项链表节点 + /// + /// + class LinkedNode + { + public T value; + //存储下一个元素 + public LinkedNode nextNode; + public LinkedNode(T value) + { + this.value = value; + } + } + /// + /// 单项链表类,管理节点 + /// + /// + class LinkedList + { + public LinkedNode head; + public LinkedNode last; + public void Add(T value) + { + LinkedNode node = new LinkedNode(value); + if (head == null) + { + head = node; + last = node; + } + else + { + last.nextNode = node; + last = node; + } + } + public void Remove(T value) + { + if (head == null) + { + return; + } + if (head.value.Equals(value)) + { + head = head.nextNode; + //如果头节点被移除了,那么证明只有一个节点,那么尾也要清空 + if (head == null) + { + last = null; + } + return; + } + LinkedNode node = head; + while (node.nextNode != null) + { + if (node.nextNode.value.Equals(value)) + { + node.nextNode = node.nextNode.nextNode; + break; + } + } + } + + } + #endregion + + #region 知识点六 顺序存储和链式存储的优缺点 + //从增删查改的角度去思考 + //增:链式存储 计算上 优于顺序存储 (中间插入时链式不用像顺序一样去移动位置) + //删:链式存储 计算上 优于顺序存储 (中间删除时链式不用像顺序一样去移动位置) + //查:顺序存储 使用上 优于链式存储 (数组可以直接通过下标得到元素,链式需要遍历) + //改:顺序存储 使用上 优于链式存储 (数组可以直接通过下标得到元素,链式需要遍历) + #endregion +} diff --git a/C#进阶/Lesson9_顺序存储和链式存储练习/Lesson9_顺序存储和链式存储练习.csproj b/C#进阶/Lesson9_顺序存储和链式存储练习/Lesson9_顺序存储和链式存储练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson9_顺序存储和链式存储练习/Lesson9_顺序存储和链式存储练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson9_顺序存储和链式存储练习/Program.cs b/C#进阶/Lesson9_顺序存储和链式存储练习/Program.cs new file mode 100644 index 0000000..2f32d97 --- /dev/null +++ b/C#进阶/Lesson9_顺序存储和链式存储练习/Program.cs @@ -0,0 +1,174 @@ +namespace Lesson9_顺序存储和链式存储练习 +{ + class LinkNode + { + public T value; + public LinkNode nextNode; + public LinkNode frontNode; + public LinkNode(T value) + { + this.value = value; + nextNode = null; + frontNode = null; + } + } + class LinkList + { + private LinkNode head; + private LinkNode tail; + private int count; + public LinkList() + { + count = 0; + head = null; + tail = null; + } + public LinkNode Head + { + get + { + return head; + } + } + public LinkNode Tail + { + get + { + return tail; + } + } + public int Count + { + get + { + return count; + } + } + public void Add(T value) + { + LinkNode node = new LinkNode(value); + if (head == null) + { + head = node; + tail = node; + Console.WriteLine("空,已加节点并赋值"); + } + else + { + tail.nextNode = node; + tail.nextNode.frontNode = tail; + tail = node; + tail.nextNode = null; + Console.WriteLine("添加尾部节点"); + } + ++count; + } + public void PrintAllNodesFront() + { + if (head == null) + { + Console.WriteLine("链表为空"); + return; + } + LinkNode node = head; + while (node != null) + { + Console.Write(node.value + " "); + node = node.nextNode; + } + } + public void PrintAllNodesLast() + { + if (head == null) + { + Console.WriteLine("链表为空"); + return; + } + LinkNode node = tail; + while (node != null) + { + Console.Write(node.value + " "); + node = node.frontNode; + } + } + public void RemoveAt(int index) + { + if (index < 0 || index >= count) + { + Console.WriteLine($"目前只有{count}个node,输入无效"); + return; + } + int tempCount = 0; + LinkNode tempNode = head; + while (true) + { + if (tempCount == index) + { + //找到节点 + if (tempNode.frontNode != null) + { + tempNode.frontNode.nextNode = tempNode.nextNode; + } + if (tempNode.nextNode != null) + { + tempNode.nextNode.frontNode = tempNode.frontNode; + } + if (index == 0) + { + head = tempNode.nextNode; + }else if (index == count - 1) + { + tail = tail.frontNode; + } + + count--; + break; + } + tempNode = tempNode.nextNode; + tempCount++; + } + } + } + class Program + { + static void Main(string[] args) + { + LinkList list = new LinkList(); + list.Add(1); + list.Add(2); + list.Add(3); + list.Add(4); + + Console.WriteLine("Node数量:"); + Console.WriteLine(list.Count); + + Console.WriteLine("-------------------"); + + Console.WriteLine("反向遍历"); + list.PrintAllNodesLast(); + + Console.WriteLine(); + Console.WriteLine("-------------------"); + + Console.WriteLine("正向遍历"); + list.PrintAllNodesFront();//正向遍历 + + Console.WriteLine(); + Console.WriteLine("-------------------"); + + Console.WriteLine("删除后正向遍历"); + list.RemoveAt(0); + list.PrintAllNodesFront();//正向遍历 + + Console.WriteLine(); + Console.WriteLine("-------------------"); + + Console.WriteLine("删除后反向遍历"); + list.RemoveAt(1); + list.PrintAllNodesFront();//正向遍历 + + Console.WriteLine(); + Console.WriteLine("-------------------"); + } + } +}