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;
}
Possible duplicate of Changes between a chained list and circular chained list
– Sorack
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.
– user28595
I specified more details about the error.
– Cléo Sousa
To compare generic objects it is necessary to use the
compareTo
and not>
– Isac
@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.
– mari