pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

C#如何自定義線性節點鏈表集合

C#如何自定義線性節點鏈表集合,這篇文章主要為大家詳細介紹了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中的內容復制到自己創建的控制臺程序中)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持html5模板網。

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

這篇文章主要介紹了C# 將Access中以時間段條件查詢的數據添加到ListView中,需要的朋友可以參考下
這篇文章主要介紹了使用C#創建Windows服務的實例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
這篇文章主要介紹了C#身份證識別相關技術詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要為大家詳細介紹了C#中TCP粘包問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了C#實現的海盜分金算法,結合具體實例形式分析了海盜分金算法的原理與C#相應實現技巧,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了C#操作INI配置文件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 飞象网 - 通信人每天必上的网站 全球化工设备网—化工设备,化工机械,制药设备,环保设备的专业网络市场。 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 软文发布-新闻发布推广平台-代写文章-网络广告营销-自助发稿公司媒介星 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 高压管道冲洗清洗机_液压剪叉式升降机平台厂家-林君机电 | 小型手持气象站-空气负氧离子监测站-多要素微气象传感器-山东天合环境科技有限公司 | Pos机办理_个人商户免费POS机申请-拉卡拉办理网 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 反渗透阻垢剂-缓蚀阻垢剂厂家-循环水处理药剂-山东鲁东环保科技有限公司 | 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 上海诺狮景观规划设计有限公司 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 真空泵维修保养,普发,阿尔卡特,荏原,卡西亚玛,莱宝,爱德华干式螺杆真空泵维修-东莞比其尔真空机电设备有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 吨袋包装机|吨包秤|吨包机|集装袋包装机-烟台华恩科技 | 德州网站制作 - 网站建设设计 - seo排名优化 -「两山建站」 | 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 防爆型气象站_农业气象站_校园气象站_农业四情监测系统「山东万象环境科技有限公司」 | 水冷散热器_水冷电子散热器_大功率散热器_水冷板散热器厂家-河源市恒光辉散热器有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 精雕机-火花机-精雕机 cnc-高速精雕机-电火花机-广东鼎拓机械科技有限公司 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 |