目錄
LinkedList <T> 類 表示雙向鏈表。
該類在 System.Collections.Generic 命名空間。
有關 LinkedListNode<T> Class 類的詳情點擊這裏, 該類是 LinkedList <T> 類 中的 節點。
LinkedList <T> 是一個雙向鏈表, 其元素指向它前面和後面的元素, 如圖所示。這樣就可以通過移動到下一個元素可以正向遍歷整個鏈表, 通過移動到前一個元素可以反向遍歷整個鏈表。
鏈表的優點:
- 如果將元素插入列表的中間位置, 使用鏈表就會非常快。在插入一個元素時, 只需要修改上一個元素的Next引用和下一個元素的Previous引用, 使它們引用所插入的元素。在 List<T> 類中, 插入一個元素時, 需要移動該元素後面的所有元素。
鏈表的缺點:
- 鏈表的元素只能一個接一個地訪問, 這需要較長的時間來查找位於鏈表中間或尾部的元素。
LinkedList<T> Constructors
public LinkedList (); public LinkedList (System.Collections.Generic.IEnumerable<T> collection); protected LinkedList (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
- 如果LinkedList<T>爲空,則 First 並 Last 屬性包含
null
。- 如果 collection 沒有元素,則 LinkedList <T>爲空,First 和 Last 屬性包含 null。
static void Main(string[] args) { string[] temp = { "huang", "cheng", "tao" }; var numbers = new LinkedList<string>(); // 使用默認構造函數初始化,該實例爲空並且具有默認初始容量。 // 該實例包含從指定集合複製的元素並且具有足夠的容量來容納所複製的元素。 var queueCopy = new LinkedList<string>(temp); var queueCopy2 = new LinkedList<string>(new string[] { "sda", "da" }); }
Propertys
LinkedList<T>.Count
public int Count { get; }
- 獲取 LinkedList<T> 中所有的元素數。
- 此操作的複雜度爲 O(1)。
LinkedList<T>.First
public System.Collections.Generic.LinkedListNode<T> First { get; }
- 獲取 LinkedList<T> 的第一個節點元素。
- 返回的是 LinkedList <T>的中的 第一個 LinkedListNode<T> 類型的元素值。
- 如果LinkedList<T>爲空,First並Last屬性包含
null
。- 檢索此屬性的值的運算複雜度爲 O(1)。
static void Main(string[] args) { var numbers = new LinkedList<string>(new string[] { "huang","cheng","tao"}) ; WriteLine(numbers.First.Value); }
LinkedList<T>.Last
- 獲取 LinkedList<T> 的最後一個節點元素。
- 返回的是 LinkedList <T>的中的 最後一個 LinkedListNode<T> 類型的元素值。
- 如果LinkedList<T>爲空,First 並Last 屬性包含
null
。- 檢索此屬性的值的運算複雜度爲 O(1)。
static void Main(string[] args) { var numbers = new LinkedList<string>(new string[] { "huang","cheng","tao"}) ; WriteLine(numbers.Last.Value); }
Methods
LinkedList<T>.AddAfter
public void AddAfter (System.Collections.Generic.LinkedListNode<T> node, System.Collections.Generic.LinkedListNode<T> newNode);
- 在 LinkedList<T> 中的現有的 node 節點之後 插入 newNode 節點。
如果參數 node
爲null
。或 -newNode
爲null
。 拋出 ArgumentNullException如果 參數 node
不在當前 LinkedList<T> 中。或 -newNode
屬於另一個 LinkedList<T> 中的節點。 拋出 InvalidOperationException- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words ={ "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); // 首先要創建一個 LinkedListNode 類型的節點,然後插入到 LinkedList 中 var mark1 = new LinkedListNode<string>("huang"); sentence.AddAfter(sentence.First, mark1); foreach(var tt in sentence) { WriteLine(tt); } } the huang fox jumps over the dog
【推薦使用】
public System.Collections.Generic.LinkedListNode<T> AddAfter (System.Collections.Generic.LinkedListNode<T> node, T value);
- 在 LinkedList<T> 中現有的 node 節點之後添加包含 value 的新節點。
如果參數 node
爲null
。 拋出 ArgumentNullException如果 參數 node
不在當前 LinkedList<T> 中。拋出 InvalidOperationException- 此方法爲 o (1) 運算。
static void Main(string[] args) { // Create the link list. string[] words ={ "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); WriteLine(sentence.AddAfter(sentence.First, "old").Value); WriteLine(); foreach (var tt in sentence) { WriteLine(tt); } } old the old fox jumps over the dog
LinkedList<T>.AddBefore
public void AddBefore (System.Collections.Generic.LinkedListNode<T> node, System.Collections.Generic.LinkedListNode<T> newNode);
- 在 LinkedList<T> 中的現有的 node 節點之前 插入 newNode 節點。
如果參數 node
爲null
。或 -newNode
爲null
。 拋出 ArgumentNullException如果 參數 node
不在當前 LinkedList<T> 中。或 -newNode
屬於另一個 LinkedList<T> 中的節點。 拋出 InvalidOperationException- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); // 首先要創建一個 LinkedListNode 類型的節點,然後插入到 LinkedList 中 var mark1 = new LinkedListNode<string>("huang"); sentence.AddBefore(sentence.First, mark1); foreach (var tt in sentence) { WriteLine(tt); } } huang the fox jumps over the dog
【推薦使用】
public System.Collections.Generic.LinkedListNode<T> AddBefore (System.Collections.Generic.LinkedListNode<T> node, T value);
- 在 LinkedList<T> 中現有的 node 節點之前添加包含 value 的新節點。
如果參數 node
爲null
。 拋出 ArgumentNullException如果 參數 node
不在當前 LinkedList<T> 中。拋出 InvalidOperationException- 此方法爲 o (1) 運算。
static void Main(string[] args) { // Create the link list. string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); WriteLine(sentence.AddBefore(sentence.First, "old").Value); WriteLine(); foreach (var tt in sentence) { WriteLine(tt); } }
LinkedList<T>.AddFirst
public void AddFirst (System.Collections.Generic.LinkedListNode<T> node);
- 在 LinkedList<T> 的隊頭添加名爲 node 的新節點。
如果參數 node
爲null
。 拋出 ArgumentNullException如果參數 node
屬於另一個 LinkedList<T>。 拋出 InvalidOperationException- 如果當前的 LinkedList<T>是空的,則此時添加的新節點將即是First,也是 Last。
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); var headPushElem = new LinkedListNode<string>("huangchengtao"); sentence.AddFirst(headPushElem); foreach (var tt in sentence) { WriteLine(tt); } } huangchengtao the fox jumps over the dog
public System.Collections.Generic.LinkedListNode<T> AddFirst (T value);
- 在 LinkedList<T> 的隊頭處添加包含 value 的新節點。
- 如果當前的 LinkedList<T>是空的,則此時添加的新節點將即是First,也是 Last。
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.AddFirst("huangchengtao"); foreach (var tt in sentence) { WriteLine(tt); } }
LinkedList<T>.AddLast
public void AddLast (System.Collections.Generic.LinkedListNode<T> node);
- 在 LinkedList<T> 的隊尾處添加名爲 node 的 新節點
如果參數 node
爲null
。 拋出 ArgumentNullException如果參數 node
屬於另一個 LinkedList<T>。 拋出 InvalidOperationException
- 如果當前的 LinkedList<T>是空的,則此時添加的新節點將即是First,也是 Last。
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); var headPushElem = new LinkedListNode<string>("huangchengtao"); sentence.AddLast(headPushElem); foreach (var tt in sentence) { WriteLine(tt); } }
public System.Collections.Generic.LinkedListNode<T> AddLast (T value);
- 在 LinkedList<T> 的隊尾處添加包含 value 的新節點。
- 如果當前的 LinkedList<T>是空的,則此時添加的新節點將即是First,也是 Last。
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.AddLast("huangchengtao"); foreach (var tt in sentence) { WriteLine(tt); } }
LinkedList<T>.Clear
public void Clear ();
- 從 LinkedList<T> 中移除所有節點。
- 如果 Count 設置爲零,並且還會釋放集合元素中對其他對象的引用。此時 First 和 Last 都被設置爲
null
。- 此方法爲 O (
n
) 操作,其中n
是Count。
LinkedList<T>.Contains
public bool Contains (T value);
- 確定 value 元素 是否在 LinkedList<T> 中。 對於引用類型,該值可以爲
null
。如果在 LinkedList<T> 中找到 value ,則返回true; 否則,false。- 此方法執行線性搜索 ; 因此,此方法爲 O (
n
) 操作,其中n
是Count。static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); if(sentence.Contains("jumps")) { WriteLine("在 sentence 中"); } else { WriteLine("不在 sentence 中"); }
LinkedList<T>.Find
public System.Collections.Generic.LinkedListNode<T> Find (T value);
- 在 LinkedList<T> 中 從前往後查找第一個 值爲 value 的節點。 否則爲 null
- LinkedList <T> 從 First開始向前搜索並在 Last 結束。
- 此方法執行線性搜索;因此,此方法爲 O (
n
) 操作,其中n
是Count。static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); var findToValue = sentence.Find("dog"); WriteLine(findToValue.Value); }
LinkedList<T>.FindLast
public System.Collections.Generic.LinkedListNode<T> FindLast (T value);
在 LinkedList<T> 中 從後往前查找第一個 值爲 value 的節點。 否則爲 null
LinkedList <T>從 Last開始向後搜索,並以 First 結束。
此方法執行線性搜索;因此,此方法爲 O (
n
) 操作,其中n
是Count。
LinkedList<T>.Remove
public void Remove (System.Collections.Generic.LinkedListNode<T> node);
- 從 LinkedList<T> 中移除名爲 node 的節點。
如果參數 node
爲null
。 拋出 ArgumentNullException如果參數 node
不在當前 LinkedList<T> 中。 拋出 InvalidOperationException- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.Remove(sentence.Find("dog")); foreach(var tt in sentence) { WriteLine(tt); } }
public bool Remove (T value);
- 從 LinkedList <T> 中刪除第一個 名爲 value 的值的節點。
- 如果成功移除包含
value
元素的節點,則爲true
;否則爲false
。 如果在原始的 LinkedList<T> 中沒有找到value
,此方法也會返回false
。- 此方法執行線性搜索;因此,此方法爲 O (
n
) 操作,其中n
是Count。static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.Remove(sentence.FindLast("dog")); foreach(var tt in sentence) { WriteLine(tt); } }
LinkedList<T>.RemoveFirst
public void RemoveFirst ();
- 移除 LinkedList<T> 隊頭的的節點。
- 如果當前的 LinkedList<T> 爲空, 拋出 InvalidOperationException 異常
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.RemoveFirst(); foreach (var tt in sentence) { WriteLine(tt); } }
LinkedList<T>.RemoveLast
- 移除 LinkedList<T> 隊尾的的節點。
- 如果當前的 LinkedList<T> 爲空, 拋出 InvalidOperationException 異常
- 此方法爲 o (1) 運算。
static void Main(string[] args) { string[] words = { "the", "fox", "jumps", "over", "the", "dog" }; LinkedList<string> sentence = new LinkedList<string>(words); sentence.RemoveLast(); foreach (var tt in sentence) { WriteLine(tt); } }
End.....