如何修复删除节点并在之后转到最后一个节点?

How to fix remove node and go to last node after?

我在从用户输入中删除一个节点并正确转到最后一个节点时遇到问题,因此它会准备好在之后添加一个新节点。我正在将此代码重构为更大的实现。

但是,我无法删除该节点并转到之后的最后一个节点。这也是使用用户输入来查找要删除的正确节点。这是一个可比较类型的通用链表。

import java.util.Scanner;
import java.io.*;

class MyGenericList <T extends Comparable<T>>
{
    private  class Node<T>
     {
        T value;
        Node<T>  next;
     }   

     private Node<T> first = null;
     int count = 0;

    public void add(T element)
     {
         Node<T> newnode = new Node<T>();
         newnode.value = element;
         newnode.next = null;

        if (first == null)
        {
            first = newnode;
        }
        else
        {
            Node<T> lastnode = gotolastnode(first);
            lastnode.next = newnode;
        }
         count++;
     }

    public void remove(T element)
    {
        Node<T> nn = new Node<T>();
        Node<T> cur = first.next;
        Node<T> prev = first;

        nn.value = element; 

        boolean deleted = false;

        while(cur != null && deleted == false)
        {
               if(cur.equals(element)) //data cannot be resolved or is not a field
               {
                   prev.next = cur.next;
                   this.count--;
                   deleted = true;
               }
        }

        prev = gotolastnode(prev);
        prev.next = nn;
    }

    public T get(int pos)
    {
         Node<T> Nodeptr = first;
         int hopcount=0;
         while (hopcount < count && hopcount<pos)
         {   if(Nodeptr != null)
             {
                Nodeptr = Nodeptr.next;
             }
             hopcount++;
         }
        return  Nodeptr.value;
    }

    private Node<T> gotolastnode(Node<T> nodepointer) 
    {
       if (nodepointer== null )
        {
          return nodepointer;
        } 
        else
        {
            if (nodepointer.next == null)
               return nodepointer;
            else
                 return gotolastnode( nodepointer.next);

        }

    }
}

class Employee implements Comparable<Employee>
{
    String name;
    int age;
    @Override
    public int compareTo(Employee arg0) 
    {
        // TODO Auto-generated method stub
        return 0;
        // implement compareto method here. 
    }
    Employee( String nm, int a)
    {
        name =nm;
        age = a;
    }
}

class City implements Comparable<City>
{

    String name;
    int population;
    City( String nm, int p)
    {
        name =nm;
        population = p;
    }
    @Override
    public int compareTo(City arg0) {
        // TODO Auto-generated method stub
        return 0;
        // implement compareto method here. 
    }

}
public class GenericLinkedList
{

    public static void main(String[] args) throws IOException
    {
        MyGenericList<Employee> ml = new MyGenericList<>();

        ml.add(new Employee("john", 32));
        ml.add(new Employee("susan", 23));
        ml.add(new Employee("dale", 45));
        ml.add(new Employee("eric", 23));

        Employee e1 = ml.get(0);
       System.out.println(  "Name " + e1.name + " Age "+ e1.age );

       ml.remove(new Employee("john", 32));
       System.out.println(  "Name " + e1.name + " Age "+ e1.age );

       ml.add(new Employee("jerry", 35));
       Employee e2 = ml.get(2);
       System.out.println(  "Name " + e2.name + " Age "+ e2.age );
    }
}

您的 remove 方法的执行有误。请参阅下面的固定 remove 方法。已添加评论以解释更改。

该解决方案已通过 online Java IDE 进行了测试,经验证可以正常工作。

public void remove(T element)
{
     if(first == null) { // edge case - empty list
        return;
     }
     else if(first.value.equals(element)) { // edge case - removing the first element
        first = first.next;
        this.count--;
        return;
     }
    //Node<T> nn = new Node<T>(); // no need to create a new node, but rather remove an existing node.
    Node<T> cur = first.next;
    Node<T> prev = first;

    //nn.value = element; //no need to create a new node and set its value attribute

    boolean deleted = false;

    while(cur != null && deleted == false)
    {
           if(cur.value.equals(element)) //data cannot be resolved or is not a field
           {
               prev.next = cur.next;
               this.count--;
               deleted = true;
           }
           else { // added missing advancement of the loop iterator - cur. prev must also be advanced
             cur = cur.next;
             prev = prev.next;
           }
    }
    // This implementation adds the removed element to the end of the list, meaning
    // it is not a remove method, but rather a move to the end implementation.
    // In order to conform to what a remove method does, the last two code lines were commented out.
    //prev = gotolastnode(prev); 
    //prev.next = nn;
}

您还必须在列表使用的 Employee class(和其他 classes)中添加 equals 的覆盖实现:

class Employee implements Comparable<Employee>
{
    String name;
    int age;
    @Override
    public int compareTo(Employee arg0) 
    {
        // sort first by name, then by age
        if(name.equals(arg0.name)) {
          return age - arg0.age;
        }
        return name.compareTo(arg0.name);
    }
    Employee( String nm, int a)
    {
        name =nm;
        age = a;
    }
    @Override
    public boolean equals(Object emp) {
       boolean result = false;
       if(emp != null && emp instanceof Employee) {
          Employee e = (Employee)emp;
          result = name.equals(e.name) && (age == e.age);
       }
       return result;
    }
}