Chained list sorted with errors in add [closed]

0

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;
}
    
asked by anonymous 25.09.2017 / 19:52

1 answer

0

Assuming the code of the Node class, probably the Node getValue () returns an object of type T and not always a Double. Regardless of whether you are ordering a LinkedListOrdenada < Double > the implementation should serve for the generic type T. Furthermore T must implement Comparable so that you can use the compareTo () method.

Class LinkedListOrdenada. Note the

26.09.2017 / 16:44