2011-03-17 70 views
0

我目前在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; 
    } 
} 
} 

任何想法?

+2

究竟是什麼問題? – Oded 2011-03-17 20:50:16

+0

在節點中刪除節點並比較數據的方法在哪裏?這些也會有用。 – 2011-03-17 20:56:13

+0

這會幫助你嗎? http://stackoverflow.com/questions/768095/sorting-a-linked-list – WorldIsRound 2011-03-17 20:58:26

回答

0

我覺得你是想問:

「!我不知道的類對象名單持有什麼我該怎樣才能進行排序對象的列表,我什麼都不知道?!」

這是對這個問題的答案。

您的T類型應執行interfaceIComparable可能是你想要的。這將給你一個比較傳入的對象的方法,要求他們有一個比較方法。喜歡的東西:

public class GenericList<T> where T : System.IComparable<T> 

這樣做是確保無論泛型類你做這個名單,該類將有一個CompareTo方法,使該類的對象進行比較的那類的其他對象。比較是排序的基本必要條件。

一旦你有了它,你可以使用你最喜歡的排序算法,並使用CompareTo(T item)方法對列表進行排序。簡單的算法將包括insertion sortselection sort。雄心勃勃的可能會嘗試merge sort

如果這不是您要問的問題,請告訴我,我們可以深入瞭解您遇到的問題。

1

我想稍微改變這樣的名單:

// implement IEnumerable<T> 
public class GenericList<T> : IEnumerable<T> 
{ 
    #region Constructors 

    public GenericList() 
    { 
    } 

    public GenericList(IEnumerable<T> values) 
     : this() 
    { 
     foreach (var val in values) 
      this.AddNode(val); 
    } 

    #endregion 

    #region IEnumerable Implementations 

    public IEnumerator<T> GetEnumerator() 
    { 
     return new Enumerator(this); 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return new Enumerator(this); 
    } 

    #endregion 

    #region Nested Enumerator 

    class Enumerator : IEnumerator<T> 
    { 
     private GenericList<T> innerList; 
     private Node current; 
     private bool started; 

     public Enumerator(GenericList<T> list) 
     { 
      this.innerList = list; 
      this.current = null; 
      started = false; 
     } 

     public T Current 
     { 
      get 
      { 
       if (!started) 
        throw new InvalidOperationException("You can't ask Current before calling MoveNext()"); 
       return current.Data; 
      } 
     } 

     object System.Collections.IEnumerator.Current 
     { 
      get { return this.Current; } 
     } 

     public bool MoveNext() 
     { 
      if (!started) 
      { 
       current = innerList.head; 
       started = true; 
      } 
      else 
      { 
       current = current.Next; 
      } 
      if (current != null) 
       return true; 
      return false; 
     } 

     public void Reset() 
     { 
      started = false; 
      current = null; 
     } 

     public void Dispose() 
     { 
     } 
    } 

    #endregion 

    #region Your methods i.e. AddNode() etc. 

    //... 

    #endregion 

} 

實施IEnumerable<T>你可以使用LINQ OrderBy()OrderByDescending()方法列表上(以及它使用foreach迭代),以及新構造函數允許您可以更輕鬆地創建新的鏈接列表:

var sortedList = new GenericList<int>(unsortedList.OrderBy(x => x));