2010-04-05 39 views
1

以下程序是分爲3類的基本鏈接列表。
在測試儀類(主)我添加幾個數字到列表(排序)。
但insteed得到的數字,結果我得到的結果是:LinkedList.LinkedList
是有點問題的StringBuilder(該計劃最早是在Java的地方使用了字符串緩衝區,但應該是一樣的我覺得呢?)LinkedList錯誤的顯示(字符串生成器)

LinkedListTester.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace LinkedList 
{ 
public class LinkedListTester 
{ 
    static void Main(string[] args) 
    { 
     LinkedList ll = new LinkedList(); 
     ll.addDataSorted(5); 
     ll.addDataSorted(7); 
     ll.addDataSorted(13); 
     ll.addDataSorted(1); 
     ll.addDataSorted(17); 
     ll.addDataSorted(8); 
     Console.WriteLine(ll); 
    } 
} 

}/*LinkedList*/ 

LinkedList.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace LinkedList 
{ 
public class LinkedList 
{ 
    //toestand 
    private LinkedListNode first; 
    private LinkedListNode last; 

    //gedrag 
    public LinkedList() 
    { 
     first = null; 
     last = null; 
    } 

    public void addDataInFront(int data) 
    { 
     first = new LinkedListNode(data, first); 
     if (last == null){ 
      last = first; 
     } 
    }/*addDataInFront*/ 

    public void addDataToBack(int data) 
    { 
     if (first == null) 
     { 
      addDataInFront(data); 
     } 
     else 
     { 
      last.setNext(new LinkedListNode(data, null)); 
      last = last.getNext(); 
     } 
    }/*addDataToBack*/ 

    public void addDataSorted(int data) 
    { 
     if (first == null || first.getData() > data) 
     { 
      addDataInFront(data); 
     } 
     else 
     { 
      LinkedListNode currentNode = first; 
      while (currentNode.getNext() != null && currentNode.getNext().getData() < data) 
      { 
       currentNode = currentNode.getNext(); 
      } 

      currentNode.setNext(new LinkedListNode(data, currentNode.getNext())); 
      currentNode = currentNode.getNext(); 

      if (currentNode.getNext() == null) 
      { 
       last = currentNode; 
      } 
     } 
    }/*addDataSorted*/ 

    public String toString() 
    { 
     StringBuilder Buf = new StringBuilder(); 
     LinkedListNode currentNode = first; 
     while (currentNode != null) 
     { 
      Buf.Append(currentNode.getData()); 
      Buf.Append(' '); 
      currentNode = currentNode.getNext(); 
     } 
     return Buf.ToString(); 
    }/*toString*/ 



}/*LinkedList*/ 

}/*LinkedList*/ 

一個LinkedListNode:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace LinkedList 
{ 
public class LinkedListNode 
{ 
    //toestand 
    private int data; 
    private LinkedListNode next; 
    private LinkedListNode previous; 

    //gedrag 
    public LinkedListNode(int data, LinkedListNode next) 
    { 
     this.data = data; 
     this.next = next; 
     this.previous = null; 
    } 

    public LinkedListNode(int data, LinkedListNode next, LinkedListNode previous) 
    { 
     this.data = data; 
     this.next = next; 
     this.previous = previous; 
    } 

    public LinkedListNode getNext() 
    { 
     return next; 
    } 

    public LinkedListNode getPrevious() 
    { 
     return previous; 
    } 

    public void setNext(LinkedListNode next) 
    { 
     this.next = next; 
    } 

    public void setPrevious(LinkedListNode previous) 
    { 
     this.previous = previous; 
    } 

    public int getData() 
    { 
     return data; 
    } 


}/*LinkedListNode*/ 
}/*LinkedList*/ 
+1

目前已經在.NET中的類型安全的實現LinkedList的''。如果這是作業,請標記爲這樣。 – 2010-04-05 09:36:25

回答

2
public **override** String **ToString**() 
{ 
    StringBuilder Buf = new StringBuilder(); 
    LinkedListNode currentNode = first; 
    while (currentNode != null) 
    { 
     Buf.Append(currentNode.getData()); 
     Buf.Append(' '); 
     currentNode = currentNode.getNext(); 
    } 
    return Buf.ToString(); 
}/*toString*/ 
+0

謝謝,那正是原因! – Chris 2010-04-05 10:20:25