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ān)聯(lián)前后節(jié)點(diǎn),從而形成鏈接的節(jié)點(diǎn);
②本示例完全由博主原創(chuàng),轉(zhuǎn)載請(qǐng)注明來處。
運(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
.NET企業(yè)級(jí)項(xiàng)目中遇到的國(guó)際化問題和解決方法
這篇文章主要介紹了.NET企業(yè)級(jí)項(xiàng)目中遇到的國(guó)際化問題和解決方法,說明了理國(guó)際化問題的一些典型例子和經(jīng)驗(yàn)之談,需要的朋友可以參考下2014-07-07
C#中通過API實(shí)現(xiàn)的打印類 實(shí)例代碼
這篇文章介紹了,C#中通過API實(shí)現(xiàn)的打印類 實(shí)例代碼,有需要的朋友可以參考一下2013-08-08
C#實(shí)現(xiàn)將PDF轉(zhuǎn)為Excel的方法詳解
通常,PDF格式的文檔能支持的編輯功能不如office文檔多,針對(duì)PDF文檔里面有表格數(shù)據(jù)的,如果想要編輯表格里面的數(shù)據(jù),可以將該P(yáng)DF文檔轉(zhuǎn)為Excel格式。本文將介紹如何利用C#實(shí)現(xiàn)PDF轉(zhuǎn)Excel,需要的可以參考一下2022-04-04
Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解
這篇文章主要介紹了Unity UGUI的GridLayoutGroup網(wǎng)格布局組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C#導(dǎo)出數(shù)據(jù)到Excel文件的方法
這篇文章主要介紹了C#導(dǎo)出數(shù)據(jù)到Excel文件的方法,涉及C#操作Excel的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#調(diào)用執(zhí)行命令行窗口(CMD)的方法與技巧
在 C# 的編程世界里,我們常常會(huì)遇到需要與操作系統(tǒng)底層進(jìn)行交互的場(chǎng)景,這時(shí),調(diào)用命令行窗口(CMD)就成為了一個(gè)強(qiáng)大的工具,通過 C# 調(diào)用 CMD 都能為我們提供極大的便利,極大地拓展了 C# 應(yīng)用程序的功能邊界,本文介紹了C#中調(diào)用執(zhí)行CMD的方法與技巧2025-01-01

