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)文章
適用于WebForm Mvc的Pager分頁(yè)組件C#實(shí)現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁(yè)組件,由C#實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04
winform使用委托和事件來完成兩個(gè)窗體之間通信的實(shí)例
這篇文章介紹了winform使用委托和事件來完成兩個(gè)窗體之間通信的實(shí)例,有需要的朋友可以參考一下2013-09-09
C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法
經(jīng)常會(huì)遇到一種情況,即當(dāng)拖動(dòng)一個(gè)窗體(主窗體)時(shí),其他窗體(子窗體)隨著該窗體移動(dòng),當(dāng)拖動(dòng)子窗體時(shí),其他窗體將不跟隨移動(dòng),這就是磁性窗體,所以本文給大家介紹了C#創(chuàng)建磁性窗體的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-04-04
C#實(shí)現(xiàn)DataTable映射成Model的方法(附源碼)
這篇文章主要介紹了C#實(shí)現(xiàn)DataTable映射成Model的方法,以實(shí)例形式較為詳細(xì)的分析了DataTable映射成Model的具體步驟與相關(guān)技巧,并附帶了完整實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-11-11
C#?HttpClient超時(shí)重試機(jī)制詳解
超時(shí)重試的實(shí)現(xiàn)方式可以使用循環(huán)結(jié)構(gòu),在請(qǐng)求發(fā)起后等待一定時(shí)間,若超時(shí)未收到響應(yīng),則再次發(fā)起請(qǐng)求,循環(huán)次數(shù)可以根據(jù)實(shí)際情況進(jìn)行設(shè)置,一般建議不超過三次,這篇文章主要介紹了C#?HttpClient超時(shí)重試,需要的朋友可以參考下2023-06-06

