所以我有一個創建按字母順序排列的鏈接列表的任務。我遇到以下問題:鏈接列表 - 顯示列表和刪除不存在的東西的問題
- 在每次插入和刪除後顯示完整列表。它似乎只在每一步打印蘋果。
- 刪除橘子,不再在列表中存在的東西。我不太熟悉嘗試和捕獲異常,我覺得我做錯了。
在此先感謝,這裏是我的代碼: 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 ...
感謝所有幫助,大加讚賞。