久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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配置文件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 精品三级在线观看 | 国产一区不卡在线观看 | 97色在线视频 | 国产精品一区二区三区四区 | 国产污视频在线 | 亚洲国产精品va在线看黑人 | 狠狠干天天干 | 国产色婷婷| 人人干人人看 | 久久99视频免费观看 | 日本高清视频在线播放 | 欧美性a视频 | 久久国产精品免费 | 精品国产成人 | 国产女人叫床高潮大片免费 | 99自拍视频| 日韩精品在线视频 | 午夜激情免费视频 | 亚洲高清av | 一级片av| 午夜精品久久久久99蜜 | 久久国产99| 国产午夜精品一区二区三区嫩草 | 天天操天天插天天干 | 综合二区| cao在线| 国产精品区一区二区三区 | 国产精品久久久久久久免费观看 | 国产福利观看 | 国产精品久久久久久久久久免费看 | 国产在线播| 黄色在线观看国产 | 亚洲一区日韩 | 欧美精品成人影院 | 久久草在线视频 | 一级在线 | 国产精品成人一区二区三区 | 玖玖免费 | 国产精品性做久久久久久 | 国产精品1区2区3区 欧美 中文字幕 | 亚洲在线视频 |