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 】
相关推荐:
Laravel如何从数据库删除数据_Laravel destroy和delete方法区别
焦点电影公司作品,电影焦点结局是什么?
Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)
北京专业网站制作设计师招聘,北京白云观官方网站?
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
详解jQuery中的事件
如何快速搭建安全的FTP站点?
Laravel如何使用Service Provider服务提供者_Laravel依赖注入与容器绑定【深度】
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
JavaScript实现Fly Bird小游戏
Laravel集合Collection怎么用_Laravel集合常用函数详解
香港网站服务器数量如何影响SEO优化效果?
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
Laravel如何优雅地处理服务层_在Laravel中使用Service层和Repository层
Laravel如何自定义分页视图?(Pagination示例)
Laravel如何实现文件上传和存储?(本地与S3配置)
如何将凡科建站内容保存为本地文件?
大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?
Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
Laravel Telescope怎么调试_使用Laravel Telescope进行应用监控与调试
Bootstrap整体框架之JavaScript插件架构
Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道
Laravel Blade模板引擎语法_Laravel Blade布局继承用法
制作电商网页,电商供应链怎么做?
如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程
如何在云服务器上快速搭建个人网站?
html文件怎么打开证书错误_https协议的html打开提示不安全【指南】
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
如何为不同团队 ID 动态生成多个“认领值班”按钮
魔方云NAT建站如何实现端口转发?
清除minerd进程的简单方法
如何快速搭建高效服务器建站系统?
大连网站制作公司哪家好一点,大连买房网站哪个好?
手机怎么制作网站教程步骤,手机怎么做自己的网页链接?
如何在IIS中新建站点并配置端口与物理路径?
使用spring连接及操作mongodb3.0实例
高防服务器租用首荐平台,企业级优惠套餐快速部署
如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环
简单实现Android验证码
Linux网络带宽限制_tc配置实践解析【教程】
JavaScript如何实现音频处理_Web Audio API如何工作?
Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全
Android自定义控件实现温度旋转按钮效果
微信小程序 配置文件详细介绍
如何快速搭建二级域名独立网站?
CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】
悟空识字如何进行跟读录音_悟空识字开启麦克风权限与录音
上一篇:抖音商城哪里查风控等级
上一篇:抖音商城哪里查风控等级

