C#如何自定義線性節(jié)點(diǎn)鏈表集合
本例子實(shí)現(xiàn)了如何自定義線性節(jié)點(diǎn)集合,具體代碼如下:
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、刪除編號(hào)為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、顯示編號(hào)為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; } } }
注意:
①這里所謂的線性節(jié)點(diǎn)指定是每個(gè)節(jié)點(diǎn)之間通過(guò)關(guān)聯(lián)前后節(jié)點(diǎn),從而形成鏈接的節(jié)點(diǎn);
②本示例完全由博主原創(chuàng),轉(zhuǎn)載請(qǐng)注明來(lái)處。
運(yùn)行結(jié)果如下:
示例下載地址:C#自定義線性節(jié)點(diǎn)鏈表集合
(請(qǐng)使用VS2015打開,如果為其他版本,請(qǐng)將Program.cs中的內(nèi)容復(fù)制到自己創(chuàng)建的控制臺(tái)程序中)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:C# 添加、修改和刪除PDF書簽的實(shí)例代碼
本文標(biāo)題:C#如何自定義線性節(jié)點(diǎn)鏈表集合
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5571.html
您可能感興趣的文章
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10C#實(shí)現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#自定義事件監(jiān)聽實(shí)現(xiàn)方法
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#編程實(shí)現(xiàn)自定義熱鍵的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
- 01-10輕松學(xué)習(xí)C#的方法
- 01-10C#自定義DataGridViewColumn顯示TreeView


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置