c# 自定義泛型鏈表類的詳解
更新時(shí)間:2013年05月31日 11:45:09 作者:
本篇文章是對(duì)c#中自定義泛型鏈表類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
(1)自定義泛型鏈表類。
public class GenericList<T>
{
private class Node
{
//當(dāng)前節(jié)點(diǎn)值
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
//節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
//節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)
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;
}
}
//要在自定義泛型集合上迭代
//必須實(shí)現(xiàn)該接口
public IEnumerator<T> GetEnumerator()
{
Node current = firstNode;
while (current != null)
{
//yield return表達(dá)式以枚舉對(duì)象返回
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é)點(diǎn)值
private T data;
public T Data
{
get { return data; }
set { data = value; }
}
//節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
private Node next;
public Node Next
{
get { return next; }
set { next = value; }
}
//節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)
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;
}
}
//要在自定義泛型集合上迭代
//必須實(shí)現(xiàn)該接口
public IEnumerator<T> GetEnumerator()
{
Node current = firstNode;
while (current != null)
{
//yield return表達(dá)式以枚舉對(duì)象返回
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();
}
}
輸出如下:
您可能感興趣的文章:
- C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實(shí)例詳解
- C#定義并實(shí)現(xiàn)單鏈表實(shí)例解析
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘三 鏈表
- C#實(shí)現(xiàn)的簡(jiǎn)單鏈表類實(shí)例
- C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘四 雙向鏈表
- c#泛型學(xué)習(xí)詳解 創(chuàng)建線性鏈表
- C#數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的實(shí)例代碼
- C#實(shí)現(xiàn)單鏈表(線性表)完整實(shí)例
- C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法
- C#如何自定義線性節(jié)點(diǎn)鏈表集合
相關(guān)文章
C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細(xì)介紹了C#如何利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01基于C#實(shí)現(xiàn)語(yǔ)音識(shí)別功能詳解
在.NET4.0中,可以借助System.Speech組件讓電腦來(lái)識(shí)別我們的聲音。本文將利用該組件實(shí)現(xiàn)語(yǔ)音識(shí)別功能,文中實(shí)現(xiàn)過(guò)程講解詳細(xì),需要的可以參考一下2022-04-04C#實(shí)現(xiàn)打開(kāi)畫圖的同時(shí)載入圖片、最大化顯示畫圖窗體的方法
這篇文章主要介紹了C#實(shí)現(xiàn)打開(kāi)畫圖的同時(shí)載入圖片、最大化顯示畫圖窗體的方法,涉及C#針對(duì)窗體及圖片操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#表達(dá)式中的動(dòng)態(tài)查詢?cè)斀狻咀g】
這篇文章主要給大家介紹了關(guān)于C#表達(dá)式中動(dòng)態(tài)查詢的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01c#linq里的Skip和Take實(shí)現(xiàn)分頁(yè)或遍歷
LINQ的優(yōu)勢(shì)在于它提供了一種直觀、類型安全的方式來(lái)操作各種類型的數(shù)據(jù),查詢常需要獲取一部分?jǐn)?shù)據(jù),為了實(shí)現(xiàn)這一功能,LINQ提供了Take?和Skip運(yùn)算符,Take運(yùn)算符用于從一個(gè)序列中返回指定個(gè)數(shù)的元素,Skip運(yùn)算符用于從一個(gè)序列中跳過(guò)指定個(gè)數(shù)的元素2024-01-01