全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

C#如何自定义线性节点链表集合

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

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 


相关文章: Android自定义listview布局实现上拉加载下拉刷新功能  潮流网站制作头像软件下载,适合母子的网名有哪些?  建站OpenVZ教程与优化策略:配置指南与性能提升  网站网页制作专业公司,怎样制作自己的网页?  建站之星备案流程有哪些注意事项?  如何在企业微信快速生成手机电脑官网?  如何高效完成独享虚拟主机建站?  广州网站制作的公司,现在专门做网站的公司有没有哪几家是比较好的,性价比高,模板也多的?  如何选择美橙互联多站合一建站方案?  网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?  如何高效配置香港服务器实现快速建站?  建站主机默认首页配置指南:核心功能与访问路径优化  建站之星Pro快速搭建教程:模板选择与功能配置指南  建站主机无法访问?如何排查域名与服务器问题  建站之星安装提示数据库无法连接如何解决?  如何在IIS中配置站点IP、端口及主机头?  建站主机功能解析:服务器选择与快速搭建指南  网站制作软件免费下载安装,有哪些免费下载的软件网站?  如何在建站宝盒中设置产品搜索功能?  太平洋网站制作公司,网络用语太平洋是什么意思?  如何高效搭建专业期货交易平台网站?  广州建站公司哪家好?十大优质服务商推荐  网站制作新手教程,新手建设一个网站需要注意些什么?  武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?  详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)  韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐  贸易公司网站制作流程,出口贸易网站设计怎么做?  Swift中swift中的switch 语句  头像制作网站在线制作软件,dw网页背景图像怎么设置?  如何用免费手机建站系统零基础打造专业网站?  香港网站服务器数量如何影响SEO优化效果?  上海网站制作网站建设公司,建筑电工证网上查询系统入口?  如何高效配置IIS服务器搭建网站?  如何获取PHP WAP自助建站系统源码?  香港服务器网站卡顿?如何解决网络延迟与负载问题?  建站上传速度慢?如何优化加速网站加载效率?  c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗  智能起名网站制作软件有哪些,制作logo的软件?  小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化  微信小程序 input输入框控件详解及实例(多种示例)  C++用Dijkstra(迪杰斯特拉)算法求最短路径  如何在阿里云虚拟服务器快速搭建网站?  ,想在网上投简历,哪几个网站比较好?  宝塔Windows建站如何避免显示默认IIS页面?  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  b2c电商网站制作流程,b2c水平综合的电商平台?  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?  魔方云NAT建站如何实现端口转发?  如何通过商城自助建站源码实现零基础高效建站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。