2015-07-19 50 views
0

我必須創建一個鏈接列表,讀取一個字符串和關聯的int的文件,並在int讀取時進行排序。到目前爲止,我已經獲得了一個將元素添加到列表的方法,以及一個基本的讀取方法(但由於某種原因丟失了文件的最後一個元素),但是每次我嘗試向讀取方法添加條件時,它都會返回一個空的列表。鏈接列表從文件中讀取,排序和錯過的錯誤

我的加入方法:

public void addFirst(String name, int rank) 
{ 
    Ship newShip = new Ship(name, rank); 

    if (isEmpty()) 
    { 
     newShip.next = null; 
     newShip.prev = null; 
     last = newShip; 
     first = newShip; 
    } 
    else 
    { 
     first.next = newShip; 
     newShip.prev = first; 
     first = newShip; 
    } 
} 

而我的工作(不過關接一個)讀法:在讀

public void readFile(String filename) throws IOException 
{ 
    try 
    { 
    File inFile = new File(filename);   //inst. file import 
    Scanner read = new Scanner(inFile);   //inst. scanner object 

    while (read.hasNext())      //reads until end of text 
    { 
     String name = read.next();    //scanner reads next string, assigns to name 
     int rank = read.nextInt();    //reads next int, assigns to rank 
     addFirst(name, rank);     //enqueues ship name and rank into list 
    } 
    read.close();        //ends read when empty 
    } 

    catch(IOException exc) 
    { 
     System.out.println("Error: file not found"); 
    } 

} 

我每次添加一個條件的,而()方法,像這樣的(並有一個「0」數據文件):

while (read.hasNext())      //reads until end of text 
    { 
     String name = read.next();    //scanner reads next string, assigns to name 
     int rank = read.nextInt();    //reads next int, assigns to rank 
     if (rank == 0) 
     { 
      addFirst(name, rank);     //enqueues ship name and rank into list 
     } 
    } 

TT不甚至似乎在所有的閱讀列表。如果我無法弄清楚爲什麼添加方法被破壞,我無法開始在插入算法中添加條件。

編輯:添加一個示例數據集。我只需要弄清楚我在概念上搞砸了什麼。

SHIP1 0 SHIP2 10 ship3 27 ship4 2 ship5 7 ....

EDIT2:

好了,放棄了,現在搞清楚使用鏈表插入並只會創建一個基於標記的插入read()方法。感謝你的幫助。

+0

添加您是從文件 – Ravi

回答

2

我試過使用你的readFile方法,它好像對我很好,正確地讀取文件! 但似乎是在一個錯誤的addFirst(String name, int rank)

你實際上是在你的else條件您添加newShip作爲第二個元素(第一個元素之後)做什麼,但你不走的事實,照顧有可能是第一個元素之後的更多項目!您沒有照顧newShipnext

例如:如果您的列表是::(儘管有了代碼,您將無法制作像這樣的鏈接列表!例子只是說明)

1<-->2<-->3-->NULLfirst指向1

然後添加新元素後,4

您的鏈接將會是這個樣子::

1<-->4<-->DANGLINGfirst指向4

and

1<--2<-->3-->NULLNo pointer through which we can this part of the Linked List!

照顧我用過的尖括號,它是指針的方向!

看來你正試圖添加到鏈表的前面! 所以,你的代碼應該看起來像這樣!

public void addFirst(String name, int rank) 
{ 
    Ship newShip = new Ship(name, rank); 

    if (isEmpty()) 
    { 
     newShip.next = null; 
     newShip.prev = null; 
     last = newShip; 
     first = newShip; 
    } 
    else 
    { 
     first.prev = newShip; 
     newShip.next = first; 
     newShip.prev = NULL; // to ensure there are no dangling pointers 
     first = newShip; 
    } 
} 
3

就問這裏,因爲它太大了評論:

假設addFirst方法是添加到鏈表的頭和船舶確實有屬性nextprev,你會不會想:

if(!isEmpty){ 

    first.prev = newShip; 
    newShip.next = first; 
    first = newShip; 

} 

,或者如果你想在鏈表的尾部添加就不是你想要的:

if(!isEmpty){ 

    last.next = newShip; 
    newShip.prev = last; 

    // take out first = newShip 

} 

無論哪種方式,似乎你有什麼可能是不正確的。如我錯了請糾正我。

+0

如果他加入鏈表的尾部讀你的樣本數據,應當'last.next = newShip'和'newShip.prev = last' – user007

+0

你說的沒錯,這就是我爲粘貼代碼所得到的結果。無論如何,你顯然有更詳細的答案 – 2015-07-19 16:30:41