我必须实现双链表。prepend 方法应该在现有列表之前插入一个新元素。但我不知道如何将新元素的引用“next”与旧列表的引用“prev”链接起来。提前致谢。
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;
}