0

这给我带来了一些麻烦......我不太明白发生了什么。这是一项数据结构作业,我按照教师 UML 图的规定创建了每个课程。这似乎是一个奇怪的链表实现。我不太理解 ListNode 类中调用另一个构造函数的构造函数,也不明白为什么没有指定计数器来跟踪列表中的项目。我也不明白我写的异常类,因为讲师指定“第二个构造函数调用超类构造函数,并发送以下字符串作为其参数:(名称+“为空”);“ 我只是把那个空了。我不太明白这是如何工作的。我也完全不知道泛型是如何工作的。我做了一些阅读,但并没有弄清楚多少。我' 将发布我的代码,任何帮助将不胜感激。我请了一天假来做这件事,我觉得自己像个白痴,因为这似乎并不是特别困难。

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; 
}
}

这是列表类。

  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)
    {
    }
      }

最后是main方法和测试类:

// 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 removed\n", removedItem );
         list.print();

         removedItem = list.removeFromFront();
         System.out.printf( "\n%d removed\n", removedItem );
         list.print();

         removedItem = list.removeFromBack();
         System.out.printf( "\n%d removed\n", removedItem );
         list.print();

         removedItem = list.removeFromBack();
         System.out.printf( "\n%d removed\n", 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)
4

1 回答 1

0

首先,您的打印方法甚至不会尝试打印前导文本(“列表是:”),而是将每个项目打印在自己的行上。

鉴于此,问题似乎出在 insertAtBack 上:创建新 ListNode 的参数是向后的(新节点应该在最后一个节点 temp 节点之后出现)。

于 2013-06-25T19:08:32.380 回答