Hi, i keep getting an error saying that NaN might not have been initialized. I think its because there is something else is wrong with my code, but i am not sure what it is. I just started using linked lists and any help would be really appreciated
import java.lang.Double;
public class LinkedListOfDoubles
{
private DoubleNode head;
private DoubleNode tail;
private int size;
public static final double NaN;
public LinkedListOfDoubles()
{
this.head = null;
this.tail = null;
this.size = 0;
}
public void addFront(double d)
{
if(head == null){
DoubleNode newNode;
newNode = new DoubleNode(d, head, null);
newNode.setNext(head);
head = newNode;
tail = head;
size++;
}else{
DoubleNode newNode;
newNode = new DoubleNode(d, head, null);
newNode.setNext(head);
head = newNode;
size++;
}
}
public void addBack(double d)
{
if(tail == null){
addFront(d);
}else{
DoubleNode newNode;
newNode = new DoubleNode(d, null, tail);
tail.setNext(newNode);
tail = newNode;
size++;
}
}
public void remove(double d)
{
if(size()== 1){
head = null;
tail = null;
size--;
}else if(size > 1){
for(DoubleNode x = head; x.next() != null; x.next())
{
if(x.getItem() == d)
{
x.setItem(x.next().getItem());
size--;
}
}
}
}
public void removeThreshold(double d, double threshold)
{
DoubleNode compare = head;
double d1 = d + threshold;
double d2 = d - threshold;
if(compare.getItem() >= d2 && compare.getItem() <= d1 ){
compare.setItem(compare.next().getItem());
size--;
}
}
public int size()
{
return size;
}
public int withinThreshold(double d, double threshold)
{
int count = 0;
double d1 = d + threshold;
double d2 = d - threshold;
for(DoubleNode compare = head; compare.next() != null; compare = compare.next()){
if(compare.getItem() >= d2 && compare.getItem() <= d1){
count++;
}
compare = compare.next();
}
return count;
}
public double maximum()
{
DoubleNode compare = head;
if(size >= 1){
while(compare.next() != null){
if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
compare = compare.next();
}
}
return compare.getItem();
}else{
return Double.NaN;
}
}
public double minimum()
{
DoubleNode compare = head;
if(size >= 1){
while(compare.next() != null){
if(compare.next().getItem() > compare.getItem() || compare.next ().getItem() == compare.getItem()){
compare = compare.next();
}
}
return compare.getItem();
}else{
return Double.NaN;
}
}
public double mean()
{
if(size() == 0){
return Double.NaN;
}else{
double mean = 0.0;
DoubleNode curr = head;
mean += curr.getItem();
if (size >= 1){
curr = curr.next();
mean += curr.getItem();
}
return (mean / size);
}
}
public double elementAt(int index)
{
DoubleNode temp;
temp = head;
if(index >= 0 && index < size())
{
for(int i =0; i < index; i++)
{
temp = temp.next();
}
return temp.getItem();
}
return Double.NaN;
}
public String toString()
{
DoubleNode curr = head;
String list = "{";
list += curr;
while(curr == null){
list += ", ";
curr = curr.next();
list += curr;
}
list += "}";
return list;
}
}