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

问题描述 投票:1回答:1

我无法从用户输入中删除节点并正确转到最后一个节点,因此可以在之后添加新节点。我正在将此代码重构为更大的实现。 但是,我无法删除节点并转到最后一个节点。这也是使用用户输入来查找要删除的正确节点。这是类似类型的通用链接列表。

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 );
    }
}
java generic-list
1个回答
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;
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.