带有通用类型故障的链接列表

时间:2013-06-25 18:57:04

标签: java generics data-structures linked-list

这给了我一些麻烦......我不太明白发生了什么。这是一个数据结构分配,我已根据教师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 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)

1 个答案:

答案 0 :(得分:0)

首先,您的打印方法甚至不会尝试打印前导文本(“列表为:”),并且它会在各自的行上打印每个项目。

鉴于此,看起来问题在于insertAtBack:使新的ListNode成为反向的参数(新节点应该在最后一个节点,临时节点之后)。

相关问题