Chained list sorted with errors in add

Asked

Viewed 151 times

0

I have the following sorted chained list, but am getting errors in my first method (add). The error message appears in all 3 interactions where I compare the values of the nodes with the operator ">" because java says it is not an appropriate operator for the argument type, and the value returned in . getValue() is a Double. Any suggestions on how to fix this to make 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;
}
  • What errors? All your questions are related to this list, and in none you added the error or the stack of errors. Make life easier for those who want to help, don’t report an error in the way Enerica, add the stack of errors if you have an exception, or be more specific about which error occurs and how it occurs.

  • I specified more details about the error.

  • To compare generic objects it is necessary to use the compareTo and not >

  • 1

    @Cléo Souza you must also paste here the code of the Node class. Even assuming what the Node<T> class is like (with the attributes T value, Node next, Node Previous) its ordered Linkedlistlist code is full of errors (e.g. overriden method signatures are wrong, Node.setNext(Aux.getValue()), missing implementing a List method, first() and last() should not have override because they are not in the List interface). Use a syntax error-tracking IDE to help you see all of these errors that need to be resolved.

1 answer

0


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

Ordered Linkedlistclass. Note the definition of <T extends Comparable> that allows us to compare 'Node.getValue(). compareTo(list.getValue()) > 0'.

//Lista genérica encadeada
public class LinkedListOrdenada<T extends Comparable> implements List<T> {
    private Node<T> list;
    private Node<T> ultimo;

    //Seus metodos que nao devem ter @Override - first(), last()....
    //....

    // e os metodos que a interface exige
    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean contains(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean add(T e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean remove(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public T get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public T set(int index, T element) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void add(int index, T element) {
        // TODO Auto-generated method stub

    }

    @Override
    public T remove(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int indexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public ListIterator<T> listIterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<T> subList(int fromIndex, int toIndex) {
        // TODO Auto-generated method stub
        return null;
    }

}

Node class:

 public class Node<T> {

        private T value;
        private Node<T> next;
        private Node<T> previous;

        public Node<T> getNext() {
            return next;
        }

        public void setNext(Node<T> next) {
            this.next = next;
        }

        public Node<T> getPrevious() {
            return previous;
        }

        public void setPrevious(Node<T> previous) {
            this.previous = previous;
        }

        public T getValue() {
            return value;
        }

        public void setValue(T value) {
            this.value = value;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.