Finding min in linked list
There is something wrong with my min() method, I got max to work though.
Code :
public double maximum()
{
DoubleNode compare = head;
if(size() == 1){
return compare.getItem();
}else 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;
Double i = 0.0;
if(size() == 1){
return compare.getItem();
}else 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;
}
}
Re: Finding min in linked list
as i studied your code acc to me your max method will also not work for every case.
Quote:
if(size() == 1){
return compare.getItem();
}else if(size() >= 1){
while(compare.next() != null){
if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
compare = compare.next();
}
}
Quote:
}else if(size() >= 1){
why you are writing size()>=1
your above condition is already checking for size==1.
Quote:
while(compare.next() != null){
if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
compare = compare.next();
}
whether it will move further if the element at next is smaller than the current.
try this input for your max function
2,23,34,32,11
Hope you will got the right path , where i want to direct you.