从链表中删除超出范围的节点

Delete out of range nodes from linked list

代码的目的是从我收到的链表中删除不在我给定范围内(Min和Max)的节点。 现在,在我为高度输入的当前值下,代码可以工作,但是如果将 f1 节点的高度更改为 80(超出范围),您将看到代码将无法工作(将不正确)。

我知道删除(方法本身)中的某些内容不起作用,我很乐意获得帮助。

这是我的代码:

节点:

public class Node<T> 
{
    private T value;
    private Node<T> next;
    
    //constructors  
    public Node(T value) 
    {
        this.value = value;
        this.next = null;
    }
    public Node(T value, Node<T> next) 
    {
        this.value = value;
        this.next = next;
    }
    
    //getters setters
    public T getValue() {
        return this.value;
    }
    public void setValue(T value) {
        this.value = value;
    }
    public Node<T> getNext() {
        return this.next;
    }
    public void setNext(Node<T> next) {
        this.next = next;
    }
    
    public boolean hasNext()
    {
        return (this.getNext()!=null);
    }
    @Override
    public String toString() {
        return "Node [value=" + this.value + ", next=" + this.next + "]";
    }
    
}

花:

public class Flower {
    private String name;
    private double height;
    private String color;
    private String season;
    
    public Flower(String name, double height, String color, String season)
    {
        this.name = name;
        this.height = height;
        this.color = color;
        this.season = season;
    }
    
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSeason() {
        return season;
    }

    public void setSeason(String season) {
        this.season = season;
    }

    public String toString()
    {
        return this.name + "[" + this.height + " cm, " + color + ", " + season+"]";
    }
}

FlowerMain(哪里出错了):

import java.util.Scanner;

public class FlowerMain {

    public static void main(String[] args) {
        Node<Flower> f1 = new Node<Flower>(new Flower("MoranA", 56.46,"Red", "Summer"));
        Node<Flower> f2 = new Node<Flower>(new Flower("MoranB", 55.46,"Red", "Winter"),f1);
        Node<Flower> f3 = new Node<Flower>(new Flower("MoranC", -57.46,"Blue", "Summer"),f2);
        Node<Flower> f4 = new Node<Flower>(new Flower("MoranD", 554.46,"Redd", "Winter"),f3);
        Node<Flower> f5 = new Node<Flower>(new Flower("MoranE", 6747.36,"Red", "Summerr"),f4);
        removeFlowersRange(f5,45,60);
    }
    
    //exc 3
    public static void removeFlowersRange(Node<Flower> flowers, double min, double max)
    {
        
        Node<Flower> current = flowers, prev = null;
        System.out.println();
        System.out.println(flowers);
        while(current!= null)
        {
            if(current.getValue().getHeight()<min || current.getValue().getHeight()>max)
            {
                prev = flowers;
                flowers = flowers.getNext();
            }
            current = current.getNext();
        }
        System.out.println();
        System.out.println(flowers);
    }
}

您永远不会在 removeFlowersRange 方法中调用 setNext(Node<T> next) 方法,因此链表永远不会改变。

当 if 条件为真时,您可能想要删除该节点,同时确保它在从列表开头删除时和从其他位置删除时有效。

public static Node<Flower> removeFlowersRange(Node<Flower> flowers, double min, double max)
{
    Node<Flower> current = flowers, prev = null;
    System.out.println();
    System.out.println(flowers);
    while(current != null)
    {
        if(current.getValue().getHeight()<min || current.getValue().getHeight()>max)
        {
            // move to the next item
            current = current.getNext();
            if (prev == null) {
                // if we have to remove an item at the start of the list,
                // just move the head of the list, that will drop the first item
                flowers = current;
            } else {
                // otherwise, drop the current item by updating
                // the previous item's next reference
                prev.setNext(current);
            }
        }
        else {
            // if the current item should not be removed
            // just move to the next item normally 
            // (not modifying the linked list)
            prev = current;
            current = current.getNext();
        }
    }
    System.out.println();
    System.out.println(flowers);

    // since when the item(s) at the start of the list are removed
    // the head of the list changes, we have to return it from this
    // method call if we want to use the changed list anywhere else
    return flowers;
}