如何解决删除节点并在之后到达最后一个节点的问题?

时间:2019-04-20 01:19:36

标签: java generic-list

我在从用户输入中删除一个节点并正确地转到最后一个节点时遇到了麻烦,因此可以在之后添加新节点。我将此代码重构为更大的实现。

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

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

1 个答案:

答案 0 :(得分:1)

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

还必须在列表所使用的equals类(和其他类)中添加Employee的重写实现:

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