C#如何自定义线性节点链表集合
发布时间 - 2026-01-11 02:19:14 点击率:次本例子实现了如何自定义线性节点集合,具体代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
namespace LineNodeDemo
{
class Program
{
static void Main(string[] args)
{
LineNodeCollection lineNodes = new LineNodeCollection();
lineNodes.Add(new LineNode("N1") { Name = "Microsoft" });
lineNodes.Add(new LineNode("N2") { Name = "Lenovo" });
lineNodes.Add(new LineNode("N3") { Name = "Apple" });
lineNodes.Add(new LineNode("N4") { Name = "China Mobile" });
Console.WriteLine("1、显示全部:");
lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
Console.WriteLine("2、删除编号为N2的元素:");
lineNodes.Remove("N2");
lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
Console.WriteLine("3、删除索引为1的元素:");
lineNodes.RemoveAt(1);
lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
Console.WriteLine("4、显示索引为0元素的名称:");
Console.WriteLine(lineNodes[0].Name);
Console.WriteLine("5、显示编号为N4元素的名称:");
Console.WriteLine(lineNodes["N4"].Name);
Console.WriteLine("6、清空");
lineNodes.Clear();
lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
Console.ReadKey();
}
}
static class Utility
{
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach(var r in source)
{
action(r);
}
}
}
class LineNodeCollection : IEnumerable<LineNode>
{
public IEnumerator<LineNode> GetEnumerator()
{
return new LineNodeEnumerator(this.firstLineNode);
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public LineNode this[int index]
{
get
{
LineNode _lineNode= this.firstLineNode;
for (int i=0;i<=index;i++)
{
if (_lineNode != null)
{
if(i<index)
{
_lineNode = _lineNode.Next;
continue;
}
else
{
return _lineNode;
}
}
else break;
}
throw new IndexOutOfRangeException("超出集合索引范围");
}
}
public LineNode this[string id]
{
get
{
LineNode _lineNode = this.firstLineNode;
for (int i = 0; i < this.Count; i++)
{
if(_lineNode.ID == id)
{
return _lineNode;
}
else
{
_lineNode = _lineNode.Next;
}
}
throw new IndexOutOfRangeException("未能在集合中找到该元素");
}
}
LineNode firstLineNode;
LineNode lastLineNode;
public void Add(LineNode lineNode)
{
this.Count++;
if(this.firstLineNode == null)
{
this.firstLineNode = lineNode;
}
else
{
if (this.firstLineNode.Next == null)
{
lineNode.Previous = this.firstLineNode;
this.firstLineNode.Next = lineNode;
this.lastLineNode = this.firstLineNode.Next;
}
else
{
lineNode.Previous = this.lastLineNode;
this.lastLineNode.Next = lineNode;
this.lastLineNode = this.lastLineNode.Next;
}
}
}
public int Count
{
private set;get;
}
public int IndexOf(string id)
{
LineNode _lineNode = this.firstLineNode;
for (int i = 0; i < this.Count; i++)
{
if (_lineNode.ID == id)
{
return i;
}
else
{
_lineNode = _lineNode.Next;
}
}
throw new IndexOutOfRangeException("未能在集合中找到该元素");
}
public void Clear()
{
this.firstLineNode = null;
this.lastLineNode = null;
this.Count = 0;
this.GetEnumerator().Reset();
}
public void RemoveAt(int index)
{
if (this.Count < index) throw new InvalidOperationException("超出集合索引范围");
LineNode _currentLineNode = this[index];
if (_currentLineNode.Previous == null)
{
_currentLineNode = _currentLineNode.Next;
this.firstLineNode = _currentLineNode;
}
else
{
LineNode _lineNode = this.firstLineNode;
for (int i = 0; i <= index - 1; i++)
{
if (i < index - 1)
{
_lineNode = _lineNode.Next;
continue;
}
if (i == index - 1)
{
if (_currentLineNode.Next != null)
{
_lineNode.Next = _lineNode.Next.Next;
}
else
{
this.lastLineNode = _lineNode;
_lineNode.Next = null;
}
break;
}
}
}
this.Count--;
}
public void Remove(string id)
{
int _index = this.IndexOf(id);
this.RemoveAt(_index);
}
public LineNode TopLineNode { get { return this.firstLineNode; } }
public LineNode LastLineNode { get { return this.lastLineNode; } }
}
class LineNodeEnumerator : IEnumerator<LineNode>
{
LineNode topLineNode;
public LineNodeEnumerator(LineNode topLineNode)
{
this.topLineNode = topLineNode;
}
public LineNode Current
{
get
{
return this.lineNode;
}
}
object IEnumerator.Current
{
get
{
return this.Current;
}
}
public void Dispose()
{
this.lineNode = null;
}
LineNode lineNode;
public bool MoveNext()
{
if(this.lineNode == null)
{
this.lineNode = this.topLineNode;
if (this.lineNode == null) return false;
if (this.lineNode.Next == null)
return false;
else
return true;
}
else
{
if(this.lineNode.Next !=null)
{
this.lineNode = this.lineNode.Next;
return true;
}
return false;
}
}
public void Reset()
{
this.lineNode = null;
}
}
class LineNode
{
public LineNode(string id)
{
this.ID = id;
}
public string ID { private set; get; }
public string Name {set; get; }
public LineNode Next { set; get; }
public LineNode Previous { set; get; }
}
}
注意:
①这里所谓的线性节点指定是每个节点之间通过关联前后节点,从而形成链接的节点;
②本示例完全由博主原创,转载请注明来处。
运行结果如下:
示例下载地址:C#自定义线性节点链表集合
(请使用VS2015打开,如果为其他版本,请将Program.cs中的内容复制到自己创建的控制台程序中)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# C#
# 线性
# 链表
# 集合
# C语言 数据结构链表的实例(十九种操作)
# C++ 实现双向链表的实例
# C语言之复杂链表的复制详解
# C语言数据结构之双向循环链表的实例
# C++ 实现静态链表的简单实例
# 面试题快慢链表和快慢指针
# C++ 中循环链表和约瑟夫环
# C语言中双向链表和双向循环链表详解
# C语言数据结构实现链表去重的实例
# 能在
# 自定义
# 到该
# 中找
# 下载地址
# 请使用
# 请将
# 转载请注明
# 大家多多
# 清空
# 本例
# 实现了
# Apple
# China
# Console
# Mobile
# Microsoft
# Lenovo
# Clear
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Python进程池调度策略_任务分发说明【指导】
晋江文学城电脑版官网 晋江文学城网页版直接进入
Laravel如何清理系统缓存命令_Laravel清除路由配置及视图缓存的方法【总结】
Java遍历集合的三种方式
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
Laravel的.env文件有什么用_Laravel环境变量配置与管理详解
Laravel如何发送系统通知_Laravel Notifications实现多渠道消息通知
Windows11怎样设置电源计划_Windows11电源计划调整攻略【指南】
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
Laravel怎么进行数据库回滚_Laravel Migration数据库版本控制与回滚操作
QQ浏览器网页版登录入口 个人中心在线进入
Laravel如何实现多表关联模型定义_Laravel多对多关系及中间表数据存取【方法】
Swift中swift中的switch 语句
如何在万网ECS上快速搭建专属网站?
Android自定义listview布局实现上拉加载下拉刷新功能
EditPlus中的正则表达式 实战(2)
jquery插件bootstrapValidator表单验证详解
Linux安全能力提升路径_长期防护思维说明【指导】
如何为不同团队 ID 动态生成多个非值班状态按钮
Windows驱动无法加载错误解决方法_驱动签名验证失败处理步骤
Windows Hello人脸识别突然无法使用
VIVO手机上del键无效OnKeyListener不响应的原因及解决方法
通义万相免费版怎么用_通义万相免费版使用方法详细指南【教程】
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
Linux虚拟化技术教程_KVMQEMU虚拟机安装与调优
简历没回改:利用AI润色让你的文字更专业
Win11怎么修改DNS服务器 Win11设置DNS加速网络【指南】
Laravel集合Collection怎么用_Laravel集合常用函数详解
如何使用 Go 正则表达式精准提取括号内首个纯字母标识符(忽略数字与嵌套)
Android仿QQ列表左滑删除操作
如何快速上传自定义模板至建站之星?
如何用AI一键生成爆款短视频文案?小红书AI文案写作指令【教程】
在Oracle关闭情况下如何修改spfile的参数
Laravel如何生成URL和重定向?(路由助手函数)
如何在Windows虚拟主机上快速搭建网站?
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
如何在 Telegram Web View(iOS)中防止键盘遮挡底部输入框
如何用AWS免费套餐快速搭建高效网站?
Laravel如何与Inertia.js和Vue/React构建现代单页应用
HTML透明颜色代码在Angular里怎么设置_Angular透明颜色使用指南【详解】
今日头条AI怎样推荐抢票工具_今日头条AI抢票工具推荐算法与筛选【技巧】
HTML透明颜色代码怎么让下拉菜单透明_下拉菜单透明背景指南【技巧】
Laravel如何为API编写文档_Laravel API文档生成与维护方法
教你用AI将一段旋律扩展成一首完整的曲子
BootStrap整体框架之基础布局组件
如何在Ubuntu系统下快速搭建WordPress个人网站?
今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】
Laravel如何发送邮件_Laravel Mailables构建与发送邮件的简明教程
网站优化排名时,需要考虑哪些问题呢?
移动端脚本框架Hammer.js
上一篇:抖音商城哪里查风控等级
上一篇:抖音商城哪里查风控等级

