我目前在C#中使用通用鏈接列表,並且需要對列表中的節點進行排序。幫助排序列表中的節點
namespace ConsoleApplication1
{
// T is the type of data stored in a particular instance of GenericList.
public class GenericList<T>
{
private class Node
{
// Each node has a reference to the next node in the list.
public Node Next;
// Each node holds a value of type T.
public T Data;
}
// The list is initially empty.
private Node head = null;
// Add a node at the beginning of the list with t as its data value.
public void AddNode(T t)
{
Node newNode = new Node();
newNode.Next = head;
newNode.Data = t;
head = newNode;
}
// The following method returns the data value stored in the last node in
// the list. If the list is empty, the default value for type T is
// returned.
public T GetFirstAdded()
{
// The value of temp is returned as the value of the method.
// The following declaration initializes temp to the appropriate
// default value for type T. The default value is returned if the
// list is empty.
T temp = default(T);
Node current = head;
while (current != null)
{
temp = current.Data;
current = current.Next;
}
return temp;
}
}
}
任何想法?
究竟是什麼問題? – Oded 2011-03-17 20:50:16
在節點中刪除節點並比較數據的方法在哪裏?這些也會有用。 – 2011-03-17 20:56:13
這會幫助你嗎? http://stackoverflow.com/questions/768095/sorting-a-linked-list – WorldIsRound 2011-03-17 20:58:26