1
I have a package with the codes below to implement a Simply Chained List. Apparently everything is correct in the classes, but when I run my main I get back what I believe to be the memory reference of the nodes and not the value itself.
I think the error must be in the method call get
. Any idea how to solve this problem???
**********MAIN***********
public class Teste<T> extends Node<T> {
public static void main(String[] args) {
//INICIALIZANDO A LISTA
List<Double> lista = new LinkedList();
//TESTANDO SE A LISTA ESTA VAZIA OU NAO
if(lista.isEmpty())
System.out.println("lista vazia\n");
else
System.out.println("lista nao esta vazia\n");
//PREENCHENDO A LISTA
for(int i = 0;i<=4;i++) {
Integer n = new Integer(i);
lista.add(n *1.0);
}
System.out.println("\nencheu\n");
//TESTANDO SE A LISTA ESTA VAZIA DEPOIS DE PREENCHER
if(lista.isEmpty())
System.out.println("lista vazia\n");
else
System.out.println("lista nao esta vazia\n");
//IMPRIMINDO A LISTA
for (int i = 0;i<=4;i++) {
System.out.println(" "+lista.get(i));
}
System.out.println("Valor do primeiro elemento é : "+lista.first()+"\n");
System.out.println("A lista contem o valor ? "+lista.contains(2.0)+"\n");
}
}
******INTERFACE AND ADDITIONAL CLASSES**********
public interface List<T> {
void add(T obj);
void set(int position, T obj);
void remove(T obj);
void remove(int position);
T get(int position);
T first();
T last();
boolean isEmpty();
boolean contains(T obj);
String toString();
}
public class Node<T> {
private T value;
private Node<T> next;
private Node<T> previous;
public Node() {
this(null, null, null);
}
public Node(T value) {
this(value, null, null);
}
public Node(T value, Node<T> next) {
this(value, next, null);
}
public Node(T value, Node<T> next, Node<T> previous) {
this.value = value;
this.next = next;
this.previous = previous;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public void setNext(Node<T> next) {
this.next = next;
}
public Node<T> getNext() {
return next;
}
public Node<T> getPrevious() {
return previous;
}
public void setPrevious(Node<T> previous) {
this.previous = previous;
}
}
public class LinkedList<T> implements List<T> {
private Node<T> list;
public LinkedList() {
this.list = null;
}
//adicionar objeto
@Override
public void add(T obj) {
Node<T> node = new Node<>(obj);
if(list == null){
list = node;
} else {
node.setNext(list);
list = node;
}
}
//adicionar na posicao
@Override
public void set(int position, T obj) {
Node<T> node = list;
int i=0;
if(position ==0)
while(node.getNext()!=null) {
if(i-1==position) {
node.getNext().setValue(obj);
}
node=node.getNext();
i++;
}
}
//remover objeto
@Override
public void remove(T obj) {
//remover
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);
}
}
//remover na posicao
@Override
public void remove(int position) {
//remover na posicao
Node<T> node = list;
int i=1;
while(node.getNext()!=null) {
if(i-1==position) {
break;
}
node=node.getNext();
i++;
}
Node aux = node.getNext();
node.setNext(aux.getNext());
aux.setNext(null);
}
//pegar valor da posicao
@Override
public T get(int position) {
//pegar o valor da posicao
Node<T> node=list;
int i=1;
while(node.getNext()!=null) {
if(i-1==position) {
break;
}
node=node.getNext();
i++;
}
return (T) node.getNext();
}
//pegar primeiro valor da lista
@Override
public T first() {
//pegar o primeiro da lista
return (T) list;
}
//pegar ultimo valor da lista
@Override
public T last() {
Node<T> node= list;
while(node.getNext()!=null) {
node=node.getNext();
}
//ultimo elemento da lista
return (T)node.getNext();
}
//verificar se a lista esta vazia
@Override
public boolean isEmpty() {
if(list==null)
return true;
else
return false;
}
//verificar se contem o objeto na lista
@Override
public boolean contains(T obj) {
//verificar se contem
Node<T> node=list;
//retorno funciona como um break, se ele fizer o retorno o laco eh interrompido
while(node.getNext()!=null) {
if(node.getNext().getValue().equals(obj)){
return true;
}
}
return false;
}
@Override
public String toString() {
int i=list.
}
}
is all right with these changes. Thank you!!
– Cléo Sousa
Could you mark the answer as accepted? No OK below the up and down arrows.
– Gustavo Fragoso