所以我有一个创建按字母顺序排列的链接列表的作业。我遇到以下问题:
先谢谢,这是我的代码: 节点类:
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 + " ");
}
}
SortedList类:
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
}
}
以下是我得到的输出:
列表(从头到尾): 苹果
列表(从头到尾): 苹果
列表(从头到尾): 苹果
列表(从头到尾): 苹果
列表(从头到尾): 苹果
列表(从头到尾):
显示java.lang.NullPointerException ...
感谢所有的帮助,非常感谢。
答案 0 :(得分:0)
好吧,我似乎注意到你的程序存在两个问题,这可以解释你的麻烦
在类中搜索要删除的元素时,您没有使用remove方法中给出的键。你只是删除第一项(开始)。
此外,除了在列表中添加第一个元素时,insert()方法似乎不会向其添加任何新节点。您可以在程序中看到您将newNode的“下一个”节点引用到列表中的元素(“newNode.next = current;”行)但您没有在您的任何元素中引用此newNode名单。这样它永远不会被迭代。您的列表应至少有一个元素,其“下一个”节点引用此newNode。
作为建议,你应该在显示和删除列表中的项目时使用你的isEmpty()方法,这样你就可以在运行程序时避免这种异常,这是因为尝试引用空节点而导致的。 p>
此链接可以帮助您使用和实现包含链接列表的数据类型:http://algs4.cs.princeton.edu/13stacks/
关于算法的Coursera还有一个非常有趣的课程,可以帮助你进一步解决这个问题。它被称为算法第一部分。
我希望我能帮助你。随意提出更多问题,我会尽力回答。