本例子实现了如何自定义线性节点集合,具体代码如下:

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();
lineNodes.ForEach(x => { Console.WriteLine(x.Name); });
Console.WriteLine("4、显示索引为0元素的名称:");
Console.WriteLine(lineNodes[].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=;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 = ; 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 = ; 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 = ;
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 = ; i <= index - ; i++)
{
if (i < index - )
{
_lineNode = _lineNode.Next;
continue;
}
if (i == index - )
{
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; }
}
}

注意:

①这里所谓的线性节点指定是每个节点之间通过关联前后节点,从而形成链接的节点;

②本示例完全由博主原创,转载请注明来处。

运行结果如下:

示例下载地址:https://pan.baidu.com/s/1eS9UIzS

(请使用VS2015打开,如果为其他版本,请将Program.cs中的内容复制到自己创建的控制台程序中)

C# 基于泛型的自定义线性节点链表集合示例的更多相关文章

  1. Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)

    很久没有写博客了,一些读者也经常问问一些问题,不过最近我确实也很忙,除了处理日常工作外,平常主要的时间也花在了继续研究微软的实体框架(EntityFramework)方面了.这个实体框架加入了很多特性 ...

  2. VS2012 常用web.config配置解析之自定义配置节点

    在web.config文件中拥有一个用户自定义配置节点configSections,这个节点可以方便用户在web.config中随意的添加配置节点,让程序更加灵活(主要用于第三方插件的配置使用) 自定 ...

  3. C#创建自定义配置节点

    转载:http://www.educity.cn/develop/495003.html 在.Net应用程序中我们经常看到VS为我们生成的项目工程中都会含有app.config或者web.connfi ...

  4. 基于HTML5 Canvas的线性区域图表教程

    之前我们看到过很多用jQuery实现的网页图表,有些还是比较实用的.今天我们来介绍一款基于HTML5 Canvas的线性区域图表应用,这个图表应用允许你使用多组数据来同时展示,并且将数据结果以线性图的 ...

  5. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  6. C#自定义配置文件节点

    老实说,在以前没写个自定义配置节点之前,我都是写到一个很常用的节点里面,就是appSettings里add,然后再对各个节点的value值进行字符串分割操作,根据各种分割字符嵌套循环处理,后来看到一些 ...

  7. 基于 HtmlHelper 的自定义扩展Container

    基于 HtmlHelper 的自定义扩展Container Intro 基于 asp.net mvc 的权限控制系统的一部分,适用于对UI层数据呈现的控制,基于 HtmlHelper 的扩展组件 Co ...

  8. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  9. 基于django的自定义简单session功能

    基于django的自定义简单session功能 简单思路: 1.建立自定义session数据库 2.登入时将用户名和密码存入session库 3.将自定义的随机session_id写入cookie中 ...

随机推荐

  1. SqlServer PIVOT函数快速实现行转列,UNPIVOT实现列转行

    我们在写Sql语句的时候没经常会遇到将查询结果行转列,列转行的需求,拼接sql字符串,然后使用sp_executesql执行sql字符串是比较常规的一种做法.但是这样做实现起来非常复杂,而在SqlSe ...

  2. python容错

    #try: except: else: #为什么叫容错呢,先说说错误,这里说的错误并不是因为马虎或者什么原因在脚本中留下的bug,这个不能容掉,所谓容掉就是略过这个错误,要在测试时候发现并修正,需要容 ...

  3. WaitForMultipleObjects

    WaitForMultipleObjects是Windows中的一个功能非常强大的函数,几乎可以等待Windows中的所有的内核对象 函数原型为: DWORD WaitForMultipleObjec ...

  4. dns服务器测试工具

    下载地址:https://www.eatm.app/wp-content/uploads/2018/08/eDnsTest.20180810.zip

  5. T4学习- 2、创建设计时模板

    使用设计时 T4 文本模板,您可以在 Visual Studio 项目中生成程序代码和其他文件. 通常,您编写一些模板,以便它们根据来自模型的数据来改变所生成的代码. 模型是包含有关应用程序要求的关键 ...

  6. python string.md

    string 包含用于处理文本的常量和类.string模块始于Python的最早版本. 2.0版本中, 许多之前只在模块中实现的函数被转移为string对象的方法. 之后的版本中, 虽然这些函数仍然可 ...

  7. BZOJ1211:[HNOI2004]树的计数(组合数学,Prufer)

    Description 一个有n个结点的树,设它的结点分别为v1, v2, …, vn,已知第i个结点vi的度数为di,问满足这样的条件的不同的树有多少棵.给定n,d1, d2, …, dn,编程需要 ...

  8. 【转】使用Chrome Frame,彻底解决浏览器兼容问题

    本文转自http://www.ryanbay.com/?p=269,感谢该作者的总结 X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的. 通过在meta中设置 ...

  9. Dig命令解析结果

    dig -t RT NAME @NS -t RT 指定要查询的资源记录类型 NAME 需要解析的域(域名) @NS 指定那个域名服务器负责解析 [root@xss ~]# dig www.ihoney ...

  10. Sql注入测试--Sqlmap

    慕课网sqlmap学习笔记: 一.SQL注入 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 例如 (1)在url上 ...