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



代码的目的是从我收到的链表中删除不在我给出的范围(最小和最大(内的节点。现在,以当前值​​我输入了高度,代码有效,但如果您将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)方法,因此链表永远不会更改。

如果条件为true,则可能需要删除该节点,同时确保从列表开头删除和从其他位置删除时该节点都能正常工作。

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

相关内容

  • 没有找到相关文章

最新更新