c# 自定義泛型鏈表類的詳解
更新時間:2013年05月31日 11:45:09 作者:
本篇文章是對c#中自定義泛型鏈表類進行了詳細的分析介紹,需要的朋友參考下
(1)自定義泛型鏈表類。
public class GenericList<T>
{
private class Node
{
//當前節(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
{
//當前節(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)文章
C#利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細介紹了C#如何利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#實現(xiàn)打開畫圖的同時載入圖片、最大化顯示畫圖窗體的方法
這篇文章主要介紹了C#實現(xiàn)打開畫圖的同時載入圖片、最大化顯示畫圖窗體的方法,涉及C#針對窗體及圖片操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08c#linq里的Skip和Take實現(xiàn)分頁或遍歷
LINQ的優(yōu)勢在于它提供了一種直觀、類型安全的方式來操作各種類型的數(shù)據(jù),查詢常需要獲取一部分數(shù)據(jù),為了實現(xiàn)這一功能,LINQ提供了Take?和Skip運算符,Take運算符用于從一個序列中返回指定個數(shù)的元素,Skip運算符用于從一個序列中跳過指定個數(shù)的元素2024-01-01