c# 自定義泛型鏈表類的詳解
更新時間:2013年05月31日 11:45:09 作者:
本篇文章是對c#中自定義泛型鏈表類進行了詳細(xì)的分析介紹,需要的朋友參考下
(1)自定義泛型鏈表類。
public class GenericList<T>
{
private class Node
{
//當(dāng)前節(jié)點值
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
//節(jié)點的下一個節(jié)點
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
//節(jié)點的上一個節(jié)點
private Node last;
public Node Last
{
get { return last; }
set { last = value; }
}
public Node(T t)
{
data = t;
next = null;
}
}
private Node firstNode;
private Node lastNode;
public void AddNode(T t)
{
Node node = new Node(t);
node.Last = lastNode;
if (lastNode != null)
lastNode.Next = node;
lastNode = node;
if (firstNode == null)
{
firstNode = node;
}
}
//要在自定義泛型集合上迭代
//必須實現(xiàn)該接口
public IEnumerator<T> GetEnumerator()
{
Node current = firstNode;
while (current != null)
{
//yield return表達式以枚舉對象返回
yield return current.Data;
current = current.Next;
}
}
}
(2)自定義泛型鏈表類調(diào)用。
class GenericListTestTwo
{
static void Main()
{
// 類型參數(shù)為int
GenericList<int> list = new GenericList<int>();
for (int a = 0; a < 5; a++)
{
list.AddNode(a);
}
foreach (int i in list)
{
System.Console.WriteLine(i);
}
//類型參數(shù)為string
GenericList<string> strList = new GenericList<string>();
strList.AddNode("First Node");
strList.AddNode("Second Node");
foreach(string s in strList)
{
System.Console.WriteLine(s);
}
Console.Read();
}
}
輸出如下:
復(fù)制代碼 代碼如下:
public class GenericList<T>
{
private class Node
{
//當(dāng)前節(jié)點值
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
//節(jié)點的下一個節(jié)點
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
//節(jié)點的上一個節(jié)點
private Node last;
public Node Last
{
get { return last; }
set { last = value; }
}
public Node(T t)
{
data = t;
next = null;
}
}
private Node firstNode;
private Node lastNode;
public void AddNode(T t)
{
Node node = new Node(t);
node.Last = lastNode;
if (lastNode != null)
lastNode.Next = node;
lastNode = node;
if (firstNode == null)
{
firstNode = node;
}
}
//要在自定義泛型集合上迭代
//必須實現(xiàn)該接口
public IEnumerator<T> GetEnumerator()
{
Node current = firstNode;
while (current != null)
{
//yield return表達式以枚舉對象返回
yield return current.Data;
current = current.Next;
}
}
}
(2)自定義泛型鏈表類調(diào)用。
復(fù)制代碼 代碼如下:
class GenericListTestTwo
{
static void Main()
{
// 類型參數(shù)為int
GenericList<int> list = new GenericList<int>();
for (int a = 0; a < 5; a++)
{
list.AddNode(a);
}
foreach (int i in list)
{
System.Console.WriteLine(i);
}
//類型參數(shù)為string
GenericList<string> strList = new GenericList<string>();
strList.AddNode("First Node");
strList.AddNode("Second Node");
foreach(string s in strList)
{
System.Console.WriteLine(s);
}
Console.Read();
}
}
輸出如下:

您可能感興趣的文章:
相關(guān)文章
適用于WebForm Mvc的Pager分頁組件C#實現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁組件,由C#實現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04
C#實現(xiàn)DataTable映射成Model的方法(附源碼)
這篇文章主要介紹了C#實現(xiàn)DataTable映射成Model的方法,以實例形式較為詳細(xì)的分析了DataTable映射成Model的具體步驟與相關(guān)技巧,并附帶了完整實例源碼供讀者下載,需要的朋友可以參考下2015-11-11

