The purpose of the code is to delete the nodes that are not in the range I gave (Min and Max) from the linked list I received. Now, at the current values I entered for the height, the code works, but if you change the height of f1 node to 80 (out of range), you will see that the code will not work (will be incorrect).
I know it's something in the delete (in the method itself) that does not work, I would be happy to get help.
This is my code:
Node:
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 + "]";
}
}
Flower:
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 (where is the error):
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);
}
}