這篇文章主要介紹了C#集合的鏈表怎么用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#集合的鏈表怎么用文章都會有所收獲,下面我們一起來看看吧。
LinkedList<T>是一個雙向鏈表,其元素會指向它前面和后面的元素。這樣,通過移動到下一個元素可以正向遍歷鏈表,通過移動到前一個元素可以反向遍歷鏈表。

鏈表在存儲元素時,不僅要存儲元素的值,還必須存儲每個元素的下一個元素和上一個元素的信息。這就是LinkedList<T>包含LinkedListNode<T>類型的元素的原因。使用LinkedListNode<T>,可以獲得列表中的下一個和上一個元素。LinkedListNode<T>定義了屬性List,Next,Previous和Value。List屬性返回與節點相關的LinkedList<T>對象。Next和Previous屬性用于遍歷鏈表,訪問當前節點之后和之前的節點。Value屬性返回與節點相關的元素,其類型是T。
鏈表的優點是,如果將元素插入到列表的中間位置,使用鏈表就會很快。在插入一個元素時,秩序啊喲修改上一個元素的Next引用和下一個元素的Previous引用,使它們引用所插入的元素。在List<T>(http://www.dadijd.cn/article/244084.htm)中,插入一個元素,需要移動該元素后面的所以元素。
鏈表的缺點是,鏈表元素只能一個接一個的訪問,這需要較長時間來查找位于鏈表中間或尾部的元素。
LinkedList<T>類定義的成員可以訪問鏈表中的第一個和最后一個元素(First和Last);
在指定位置插入元素:AddAfter(),AddFirst()和AddLast();
刪除指定位置的元素:Remove(),RemoveFirst(),RemoveLast();
搜索:Find(),FindLast()。
下面用一個例子演示鏈表。在鏈表中,文檔按照優先級來排序。如果多個文檔的優先級相同,這些元素就按照文檔的插入時間來排序:
PriorityDocumentManager類使用一個鏈表LinkedList<Document> documentList和一個列表List<LinkedListNode<Document>> priorityNodes,鏈表包含Document對象,Document對象包含文檔的標題和優先級。列表List<LinkedListNode<Document>> priorityNodes應最多包含10個元素,每個元素分別是引用每個優先級的最后一個文檔對象。

public class PriorityDocumentManager
{
private readonly LinkedList<Document> documentList;
// priorities 0.9
private readonly List<LinkedListNode<Document>> priorityNodes;
public PriorityDocumentManager()
{
documentList = new LinkedList<Document>();
priorityNodes = new List<LinkedListNode<Document>>(10);
for (int i = 0; i < 10; i++)
{
priorityNodes.Add(new LinkedListNode<Document>(null));
}
}
public void AddDocument(Document d)
{
//Contract.Requires<ArgumentNullException>(d != null, "argument d must not be null");
if (d == null) throw new ArgumentNullException("d");
AddDocumentToPriorityNode(d, d.Priority);
}
private void AddDocumentToPriorityNode(Document doc, int priority)
{
if (priority > 9 || priority < 0)
throw new ArgumentException("Priority must be between 0 and 9");
//檢查優先級列表priorityNodes中是否有priority這個優先級
if (priorityNodes[priority].Value == null)
{
//如果沒有,遞減優先級值,遞歸AddDocumentToPriorityNode方法,檢查是否有低一級的優先級
--priority;
if (priority >= 0)
{
AddDocumentToPriorityNode(doc, priority);
}
else //如果已經沒有更低的優先級時,就直接在鏈表中添加該節點,并將這個節點添加到優先級列表
{
documentList.AddLast(doc);
priorityNodes[doc.Priority] = documentList.Last;
}
return;
}
else //優先級列表priorityNodes中有priority這個優先級
{
LinkedListNode<Document> prioNode = priorityNodes[priority];
//區分優先級列表priorityNodes存在這個指定的優先級值的節點,還是存在較低的優先級值的節點
if (priority == doc.Priority)
// 如果存在這個指定的優先級值的節點
{
//將這個節點添加到鏈表
documentList.AddAfter(prioNode, doc);
// 將這個節點賦予優先級列表中的這個優先級值的節點,因為優先級節點總是引用指定優先級節點的最后一個文檔
priorityNodes[doc.Priority] = prioNode.Next;
}
else //如果存在較低的優先級值的節點
{
//在鏈表中找到這個較低優先級的第一個節點,把要添加的節點放到它前面
LinkedListNode<Document> firstPrioNode = prioNode;
//通過循環,使用Previous找到這個優先級的第一個節點
while (firstPrioNode.Previous != null &&
firstPrioNode.Previous.Value.Priority == prioNode.Value.Priority)
{
firstPrioNode = prioNode.Previous;
prioNode = firstPrioNode;
}
documentList.AddBefore(firstPrioNode, doc);
// 設置一個新的優先級節點
priorityNodes[doc.Priority] = firstPrioNode.Previous;
}
}
}
public void DisplayAllNodes()
{
foreach (Document doc in documentList)
{
Console.WriteLine("priority: {0}, title {1}", doc.Priority, doc.Title);
}
}
// returns the document with the highest priority
// (that's first in the linked list)
public Document GetDocument()
{
Document doc = documentList.First.Value;
documentList.RemoveFirst();
return doc;
}
}
//存儲在鏈表中的元素是Document類型
public class Document
{
public string Title { get; private set; }
public string Content { get; private set; }
public byte Priority { get; private set; }
public Document(string title, string content, byte priority)
{
this.Title = title;
this.Content = content;
this.Priority = priority;
}
}客戶端代碼:
static void Main()
{
PriorityDocumentManager pdm = new PriorityDocumentManager();
pdm.AddDocument(new Document("one", "Sample", 8));
pdm.AddDocument(new Document("two", "Sample", 3));
pdm.AddDocument(new Document("three", "Sample", 4));
pdm.AddDocument(new Document("four", "Sample", 8));
pdm.AddDocument(new Document("five", "Sample", 1));
pdm.AddDocument(new Document("six", "Sample", 9));
pdm.AddDocument(new Document("seven", "Sample", 1));
pdm.AddDocument(new Document("eight", "Sample", 1));
pdm.DisplayAllNodes();
Console.ReadKey();
}關于“C#集合的鏈表怎么用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C#集合的鏈表怎么用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。