有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

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) 个答案

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

    还必须在列表使用的Employee类(和其他类)中添加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;
        }
    }