'Doubly linked list not working, specifict implementation
I need to implement a Doubly linked list, and implement a addNode method that takes 3 arguments (head, tail, value). Here is my interface:
public interface DoublyLinkedListTDA {
//void inicializar();
void agregar(
DoubleNode head,
DoubleNode tail,
int value);
}
public class DoublyLinkedList implements DoublyLinkedListTDA {
DoubleNode head;
DoubleNode tail;
public void inicializar() {
head = null;
tail = null;
}
public void agregar(DoubleNode head, DoubleNode tail, int value) {
DoubleNode NuevoNodo = new DoubleNode();
if (this.head == null) {
head = NuevoNodo;
tail = NuevoNodo;
NuevoNodo.value = value;
NuevoNodo.prev = null;
NuevoNodo.sig = null;
} else {
NuevoNodo.value = value;
tail.sig = NuevoNodo;
NuevoNodo.prev = tail;
tail = NuevoNodo;
NuevoNodo.sig = null;
}
}
}
And here is my Node Class
public class DoubleNode {
int value;
DoubleNode sig;
DoubleNode prev;
}
But this is not working properly. When testing with:
package simulacro;
public class App {
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
list.inicializar();
DoubleNode uno = new DoubleNode();
uno.value = 7;
uno.prev = null;
DoubleNode dos = new DoubleNode();
dos.value = 4;
dos.prev = uno;
dos.sig = null;
uno.sig = dos;
list.agregar(null, dos, 8);
System.out.println(list.tail.value);
}
}
Values, head, and tails are not updating properly. When I print the values of head and tails usually its returns null, and sometimes it return Node "dos" value, and not new tail value "8".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|