I have to implement double linked lists. The method prepend should insert a new element bevor the existing list. But I dont know how to link the reference "next" of the new element with the reference "prev" of the old list. Thanks in advance.
public class DoublyLinkedList {
private String info;
private DoublyLinkedList next;
private DoublyLinkedList prev;
public DoublyLinkedList(String info) {
this.info = info;
this.next = this.prev = null;
}
private DoublyLinkedList(String info, DoublyLinkedList prev, DoublyLinkedList next) {
this.info = info;
this.prev = prev;
this.next = next;
}
DoublyLinkedList prepend(String info) {
// Beginning of a list, insert new element
if (prev == null) {
prev = new DoublyLinkedList(info, null, next);
} else {
prev.prepend(info);
}
return prev;
}