2014-09-30 56 views
0

所以我有一個創建按字母順序排列的鏈接列表的任務。我遇到以下問題:鏈接列表 - 顯示列表和刪除不存在的東西的問題

  • 在每次插入和刪除後顯示完整列表。它似乎只在每一步打印蘋果。
  • 刪除橘子,不再在列表中存在的東西。我不太熟悉嘗試和捕獲異常,我覺得我做錯了。

在此先感謝,這裏是我的代碼: Node類:

class Node 
{ 
    String item; // data item 
    Node next;  // next Node in list 

    public Node(String i) 
    { 
    item = i; 
    } 

    public void displayLink() // displays this link 
    { 
    System.out.println (item + " "); 
    } 
} 

排序列表類:

class SortedList 
    { 
     Node start; // reference to the first item in the list 

     public SortedList() 
     { 
     start = null; 
     } 

     public boolean isEmpty() // true if no links 
     { 
     return (start == null); 
     } 

     public void insert(String key) 
     { 
     Node newNode = new Node(key); // creates new Node 
     Node previous = null;   // start at the beginning 
     Node current = start; 

     // until end of list, 
     while (current != null && (current.item.compareTo(newNode.item)<0)) 
     { // while current isn't null and current.item is equal to newNode.item 
      previous = current; 
      current = current.next; // go to the next item 
     } 

     if (previous == null) // at beginning of list 
      start = newNode;  // start --> newNode 
     else // not at the beginning 
      previous.next = current; // old previous --> newNode 
     newNode.next = current; // newNode --> old current 
     } 

    public Node remove(String key) // return and delete first Node 
     { 
     Node temp = start; // saves start 
     start = start.next; // deletes start 
     return temp;   // returns String 
     } 

     public void displayList() 
     { 
     System.out.println("List (first to last): "); 
     Node current = start; // starts at the beginning of the list 
     while (current != null) // until end of the list 
     { 
      current.displayLink(); // prints data 
      current = current.next; // moves to next Node 
     } 
     System.out.println(" "); 
     } 
    } 

最後我的演示類:

class SortedListDemo 
{ 
    public static void main (String [] args) 
    { 
    SortedList theSortedList = new SortedList(); // creates new list 

    theSortedList.insert("apple"); // inserts apple 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("orange"); // inserts orange 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("kiwi"); // inserts kiwi 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("tangerine"); // inserts tangerine 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("strawberry"); // inserts strawberry 
    theSortedList.displayList(); // displays list 

    theSortedList.remove("apple"); // deletes apple 
    theSortedList.displayList(); // displays list 

    theSortedList.remove("strawberry"); // deletes strawberry 
    theSortedList.displayList(); // displays list 

    theSortedList.remove("tangerine"); // deletes tangerine 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("apple"); // inserts apple 
    theSortedList.displayList(); // displays list 

    try 
    { 
    theSortedList.remove("tangerine"); // deletes tangerine 
    theSortedList.displayList(); // displays list 
    } 
    catch (NullPointerException tangerine) 
    { 
     System.out.println ("Tangerine doesn't exist, so can't be deleted"); 
    } 

    theSortedList.remove("apple"); // deletes apple 
    theSortedList.displayList(); // displays list 

    try 
    { 
    theSortedList.remove("tangerine"); // deletes tangerine 
    theSortedList.displayList(); // displays list 
    } 
    catch (NullPointerException e) 
    { 
     System.out.println ("Tangerine doesn't exist, so can't be deleted"); 
    } 

    try 
    { 
    theSortedList.remove("apple"); // deletes tangerine 
    theSortedList.displayList(); // displays list 
    } 
    catch (NullPointerException e) 
    { 
     System.out.println ("Apple doesn't exist, so can't be deleted"); 
    } 

    theSortedList.remove("kiwi"); // deletes kiwi 
    theSortedList.displayList(); // displays list 

    theSortedList.remove("orange"); // deletes orange 
    theSortedList.displayList(); // displays list 

    theSortedList.remove("strawberry"); // deletes apple 
    theSortedList.displayList(); // displays list 

    theSortedList.insert("job-well-done"); // inserts job-well-done 
    theSortedList.displayList(); // displays list 

    } 
} 

這裏是我得到的輸出:

名單(第一個到最後): 蘋果

名單(第一至最後一個): 蘋果

名單(第一個到最後): 蘋果

名單(第一個到最後): 蘋果

名單(第一至最後一個): 蘋果

李ST(第一到最後):

顯示java.lang.NullPointerException ...

感謝所有幫助,大加讚賞。

回答

0

嗯,我似乎已經注意到了兩個問題,你的程序,它可以

  1. 您沒有使用在刪除元素搜索時,在remove方法給出的解釋關鍵你的煩惱類。你只是刪除第一個項目(開始)。另外,除了在列表中添加第一個元素時,insert()方法似乎不會爲其添加任何新節點。您可以在程序中看到您將newNode的「下一個」節點引用到列表中的某個元素(「newNode.next = current;」行),但您並未在您的任何元素中引用此newNode名單。這樣它永遠不會被迭代。你的列表應該至少有一個元素的「下一個」節點引用這個newNode。

作爲一個建議,displayng並在你的列表中刪除項目時,你應該用你的isEmpty()方法,這樣就可以避免該異常,當你運行你的程序,這是probalby造成了嘗試引用空節點。

此鏈接可以幫助您使用和實現數據類型的鏈表:http://algs4.cs.princeton.edu/13stacks/

還有在Coursera一個非常有趣的過程有關的算法,幫助您進一步在這個問題上。它被稱爲算法部分I.

我希望我能夠幫助你。隨意問更多的問題,我會盡力回答他們。