I have the following chained list sorted, but I'm getting errors in my first method (add). The error message appears in all 3 interactions where I compute the values of the nodes with the ">" because java says that it is not a suitable operator for the type of argument, and that the value returned in .getValue () is a Double. Any suggestions on how to fix this to do the comparison normally?
//Lista genérica encadeada
public class LinkedListOrdenada<T> implements List<T> {
private Node<T> list;
private Node<T>ultimo;
public LinkedListOrdenada() {
this.list = null;
this.ultimo = null;
}
@Override
public void add(T obj) {
Node<T>aux = list;
Node<T> node = new Node();
if((node.getValue()) < (list.getValue()) ){
node.setNext(list);
list = node;
}else{
if((node.getValue()) > (ultimo.getValue())){
ultimo.setNext(node);
node.setNext(null);
}else{
while(aux != ultimo){
if((node.getValue()) > (aux.getValue()) ){
node.setNext(Aux.getValue());
aux.setNext(node);
break;
}
aux = aux.getNext();
}
}
}
}
@Override
public void set(int position, Object obj) {
//
}
@Override
public void remove(Object obj) {
Node<T> node = list;
if(node.getValue().equals(obj)){
list = node.getNext();
node.setNext(null);
} else {
while(node.getNext() != null){
if(node.getNext().getValue().equals(obj)){
break;
}
node = node.getNext();
}
Node aux = node.getNext();
node.setNext(aux.getNext());
aux.setNext(null);
}
}
@Override
public void remove(int position) {
Node<T> node = list;
int i = 0;
if(position == 0){
list = node.getNext();
node.setNext(null);
}else{
while(node.getNext()!= null) {
if(i == position) {
break;
}
i++;
node = node.getNext();
}
Node aux = node.getNext();
node.setNext(aux.getNext());
aux.setNext(null);
}
}
@Override
public T get(int position) {
Node<T> node = list;
int i = 0;
while(node != null) {
if(i == position) {
break;
}
i++;
node = node.getNext();
}
return node.getValue();
}
@Override
public T first() {
if(list!= null) {
return (T) list.getValue();
}
return null;
}
@Override
public T last() {
Node<T> node = list;
if(node.getNext()==null) {
return (T)node.getValue();
}else {
while(node.getNext()!=null) {
node = node.getNext();
}
return (T) node.getValue();
}
}
@Override
public boolean isEmpty() {
if(list == null) {
return true;
}else {
return false;
}
}
@Override
public boolean contains(Object obj) {
Node<T> node = list;
while(node!= null) {
if(node.getValue().equals(obj)) {
return true;
}
node = node.getNext();
}
return false;
}