在C#中,可以自定義節(jié)點(diǎn)數(shù)據(jù)類型來(lái)存儲(chǔ)在LinkedListNode<T>
中。以下是一個(gè)示例:
public class CustomData
{
public int Id { get; set; }
public string Name { get; set; }
public CustomData(int id, string name)
{
Id = id;
Name = name;
}
}
LinkedList<CustomData> linkedList = new LinkedList<CustomData>();
CustomData data1 = new CustomData(1, "Alice");
CustomData data2 = new CustomData(2, "Bob");
linkedList.AddLast(data1);
linkedList.AddLast(data2);
LinkedListNode<CustomData> currentNode = linkedList.First;
while (currentNode != null)
{
Console.WriteLine($"Id: {currentNode.Value.Id}, Name: {currentNode.Value.Name}");
currentNode = currentNode.Next;
}
在上面的示例中,我們定義了一個(gè)CustomData
類來(lái)存儲(chǔ)自定義節(jié)點(diǎn)數(shù)據(jù)。然后我們創(chuàng)建一個(gè)LinkedList<CustomData>
來(lái)存儲(chǔ)這些自定義節(jié)點(diǎn)數(shù)據(jù),并將其添加到鏈表中。最后,我們遍歷鏈表并輸出每個(gè)節(jié)點(diǎn)的數(shù)據(jù)。