我在LinkedList(通用)中的删除方法有什么问题?

时间:2015-11-06 13:46:47

标签: java class generics linked-list

我更改了LinkedList类,但它仍无法正常工作

LinearNode类

 public class LinearNode<T>{


  private LinearNode<T> next;
   private T element;

   public LinearNode()
   {
      next = null;
      element = null;
   }

   public LinearNode (T elem)
   {
      next = null;
      element = elem;
   }

   public LinearNode<T> getNext()
   {
      return next;
   }
   public void setNext (LinearNode<T> node)
   {
      next = node;
   }

   public T getElement()
   {
      return element;
   }

   public void setElement (T elem)
   {
      element = elem;
   }
}

我无法在我的java泛型类中找出delete方法的问题

public void delete(T element){

LinearNode<T> previous = list; 
LinearNode<T> current = list;
boolean found = false;

while (!found && current != null)
{
    if (current.getElement ().equals (element)) {
        found = true;
    }
    else {
        previous = current;
        current = current.getNext();
    }
}
//found loop
if (found)//we fount the element
{

    if(current == this.list){
           previous.setNext (null);
           this.last = previous;
       }

   else
       if(current == this.last){
           this.last.setNext(null);
           this.last.equals(previous.getElement()); 

       }
       else{

           previous.setNext(current.getNext());
           current.setNext (null);
       }

this.count--;
}

}

我还有我的驱动程序类,它将删除链表中的元素

这里也是驱动程序类

的一部分
public void delete(){

        Teacher aTeacher;
        Scanner scan = new Scanner(System.in);
        String number;
        aTeacher = new Teacher();

        System.out.println("Now you can delete teachers from the programme by their number.");
        System.out.println("Please input number:");
        number = scan.nextLine();

        if (aTeacher.getNumber().equals(number)){
        teachers.delete(aTeacher);
        }
        else {
            System.out.println("There are no any teacher with this number.");
        }
    }

1 个答案:

答案 0 :(得分:1)

我可以在你的代码中看到一些问题。

这个循环有点奇怪

while (current != null && !current.getElement().equals(element))
{
    previous = current;
    current = current.getNext();
    found = true;
}

你不应该在每次迭代时在循环中设置found = true,因为那样你总会相信你在循环完成后找到了元素。如果传入列表中已知的值,则不会发现问题。如果您传入的值不在列表中,那么您可能会在代码中看到current设置为null

我可能会写这个

while (! found && current != null)
{
    if (current.getElement ().equals (element)) {
        found = true;
    }
    else {
        previous = current;
        current = current.getNext();
    }
}

这个区块有点奇怪

       if(current == this.last){
           this.last.setNext(null);
           this.last.equals(previous.getElement()); 
       }

这些陈述似乎都没有任何效果。 last.getNext ()的值应为nullthis.last.equals(previous.getElement())仅测试最后一个节点是否等于倒数第二个节点中保存的元素;评估应始终为false,并且希望没有副作用。

我可能会写这个

       if(current == this.last){
           previous.setNext (null);
           this.last = previous;
       }

最后,尽管删除本身并不是问题,但我仍然会在这里彻底检查并确保被删除的节点不会保留对列表的任何引用。

所以这个

       previous.setNext(current.getNext());

可能会成为这个

       previous.setNext(current.getNext());
       current.setNext (null);