这给我带来了一些麻烦。。。我不太明白发生了什么。这是一个数据结构作业,我已经按照讲师UML图的规定创建了每个类。这似乎是一个奇怪的链表实现。我不太理解ListNode类中调用另一个构造函数的构造函数,也不理解为什么没有指定计数器来跟踪列表中的项。我也不理解我写的异常类,因为讲师指定"第二个构造函数调用超类构造函数,并发送以下字符串作为其参数:(name+"为空");"我只是把它留空。我不太明白这是怎么回事。我也完全不知道泛型是如何工作的。我读了一些书,但没有;我没怎么清理。我会发布我的代码,任何帮助都将不胜感激。我请了一天假来做这件事,我觉得自己像个十足的白痴,因为这似乎并不特别困难。
ListNode类:
package linkedLists;
public class ListNode<T> {
T data;
ListNode<T> nextNode;
ListNode(T object)
{
this(object, null);
}
ListNode(T object, ListNode<T> node)
{
data = object;
nextNode = node;
}
T getData()
{
return data;
}
ListNode<T> getNext()
{
return nextNode;
}
}
这是List类。
package linkedLists;
public class List<T>{
ListNode<T> firstNode;
ListNode<T> lastNode;
String name;
public List()
{
this("list");
}
public List(String listName)
{
name = listName;
firstNode = null;
lastNode = null;
}
public void insertAtFront(T insertItem)
{
if(isEmpty())
{
ListNode<T> node = new ListNode<T>(insertItem);
firstNode = node;
lastNode = node;
}
else
{
ListNode<T> tempNode = firstNode;
ListNode node = new ListNode(insertItem, tempNode);
firstNode = node;
}
}
public void insertAtBack(T insertItem)
{
if(isEmpty())
{
ListNode<T> node = new ListNode<T>(insertItem);
firstNode = node;
lastNode = node;
}
else
{
ListNode<T> tempNode = lastNode;
ListNode node = new ListNode(insertItem, tempNode);
lastNode = node;
}
}
public T removeFromFront() throws EmptyListException
{
if(isEmpty())
{
throw new EmptyListException("gfy");
}
else
{
ListNode<T> nr;
nr = firstNode;
if(firstNode == lastNode)
{
firstNode = null;
lastNode = null;
}
else
{
firstNode = firstNode.getNext();
}
return nr.getData();
}
}
public T removeFromBack() throws EmptyListException
{
if(isEmpty())
{
throw new EmptyListException("gfy");
}
else
{
ListNode<T> nr;
nr = lastNode;
if(firstNode == lastNode)
{
firstNode = null;
lastNode = null;
}
else
{
ListNode<T> current = firstNode;
ListNode<T> secondToLast = null;
int numOfNodes = 0;
while(current != lastNode)
{
secondToLast = current;
current = current.getNext();
}
lastNode = secondToLast;
lastNode.nextNode = null;
}
return nr.getData();
}
}
public boolean isEmpty( )
{
if(firstNode == null)
return true;
else
return false;
}
public void print()
{
if(isEmpty())
{
System.out.println("List is empty!");
return;
}
else
{
System.out.println(firstNode.getData());
ListNode<T> printNode = firstNode;
while(printNode != lastNode)
{
printNode = printNode.getNext();
System.out.println(printNode.getData());
}
}
}
}
异常类
package linkedLists;
public class EmptyListException extends RuntimeException
{
public EmptyListException()
{
this("list");
}
public EmptyListException(String name)
{
}
}
最后介绍了主要的方法和测试类别:
// ListTest.java
// ListTest class to demonstrate List capabilities.
import quiz1cs304summer2013kensotak.List;
import quiz1cs304summer2013kensotak.EmptyListException;
public class ListTest
{
public static void main( String[] args )
{
List< Integer > list = new List< Integer >(); // create a List
// insert integers in list
list.insertAtFront( -1 );
list.print();
list.insertAtFront( 0 );
list.print();
list.insertAtBack( 1 );
list.print();
list.insertAtBack( 5 );
list.print();
// remove objects from list; print after each removal
try
{
int removedItem = list.removeFromFront();
System.out.printf( "n%d removedn", removedItem );
list.print();
removedItem = list.removeFromFront();
System.out.printf( "n%d removedn", removedItem );
list.print();
removedItem = list.removeFromBack();
System.out.printf( "n%d removedn", removedItem );
list.print();
removedItem = list.removeFromBack();
System.out.printf( "n%d removedn", removedItem );
list.print();
} // end try
catch ( EmptyListException emptyListException )
{
emptyListException.printStackTrace();
} // end catch
} // end main
} // end class ListTest
应输出以下内容:
The list is: -1
The list is: 0 -1
The list is: 0 -1 1
The list is: 0 -1 1 5
0 removed
The list is: -1 1 5
-1 removed
The list is: 1 5
5 removed
The list is: 1
1 removed
Empty list
我的输出是:
-1
0
-1
0
-1
Exception in thread "main" java.lang.NullPointerException
at linkedLists.List.print(List.java:146)
at ListTest.main(ListTest.java:18)
首先,您的打印方法甚至不尝试打印前导文本("the list is:"),而是在自己的行上打印每个项目。
考虑到这一点,看起来问题出在insertAtBack上:制作新ListNode的参数是向后的(新节点应该在最后一个节点temp节点之后)。