There are probably different ways to implement your requirements. Below is my implementation. Note that I changed the names. I named the interface Listing so as not to clash with java.util.List. Also the convention for type parameters is a single, capital letter so I changed Type to T. I changed class Node to ListNode because there are already lots of Node classes. I also added a toString() method to class ListNode and also to class DoublyLinkedList as a testing aid. I also added method main() to class DoublyLinkedList to make it possible to test the implementation. Finally, I added method add() to class DoublyLinkedList to make it possible to create non-empty lists.
Explaining how the below code works would involve a lot of text and probably also several diagrams. Rather than do that, I suggest you simply run the code in a debugger. Most IDE's have a debugger.
Interface Listing
public interface Listing<T> {
/**
* Append 'list' to the end of this 'Listing'.
*
* @param list list to append.
*/
void addAll(Listing<T> list);
/**
* Insert 'list' after element at 'index'. If 'index' greater than size of this
* 'Listing', append 'list' to this 'Listing'.
*
* @param index insertion index
* @param list list to insert
*
* @return The index after which 'list' was inserted.
*/
int addAll(int index, Listing<T> list);
}
Class ListNode
public class ListNode<T> {
protected T data;
protected ListNode<T> next;
protected ListNode<T> previous;
public ListNode(T data) {
this.data = data;
}
public T getData() {
return data;
}
public ListNode<T> getNext() {
return next;
}
public ListNode<T> getPrevious() {
return previous;
}
public String toString() {
return String.format("<-%s->", String.valueOf(data));
}
}
Class DoublyLinkedList
public class DoublyLinkedList<T> implements Listing<T> {
private ListNode<T> head;
public void add(T data) {
ListNode<T> newNode = new ListNode<T>(data);
if (head == null) {
head = newNode;
}
else {
ListNode<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
newNode.previous = current;
}
}
@Override
public void addAll(Listing<T> list) {
if (list instanceof DoublyLinkedList) {
DoublyLinkedList<T> lst = (DoublyLinkedList<T>) list;
if (lst.head != null) {
if (head == null) {
head = lst.head;
}
else {
ListNode<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = lst.head;
lst.head.previous = current;
}
}
}
}
@Override
public int addAll(int index, Listing<T> list) {
if (index < 0) {
throw new IllegalArgumentException("Negative index.");
}
int counter = 0;
if (list instanceof DoublyLinkedList) {
DoublyLinkedList<T> lst = (DoublyLinkedList<T>) list;
if (lst.head != null) {
if (head == null) {
if (index == 0) {
head = lst.head;
}
}
else {
ListNode<T> current = head;
while (current.next != null && counter < index) {
counter++;
current = current.next;
}
if (counter < index) {
current.next = lst.head;
lst.head.previous = current;
}
else {
current.previous.next = lst.head;
ListNode<T> tmp = current;
ListNode<T> curr = lst.head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = tmp;
curr.previous = tmp.previous;
tmp.previous = curr;
}
}
}
}
return counter;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (head != null) {
ListNode<T> current = head;
while (current != null) {
sb.append(current);
current = current.next;
}
}
return sb.toString();
}
public static void main(String[] args) {
DoublyLinkedList<String> list1 = new DoublyLinkedList<>();
list1.add("One");
DoublyLinkedList<String> list2 = new DoublyLinkedList<>();
list2.add("First");
list1.addAll(list2);
System.out.println(list1);
DoublyLinkedList<String> list3 = new DoublyLinkedList<>();
list3.add("TOO");
int result = list1.addAll(1, list3);
System.out.printf("[%d] %s%n", result, list1);
}
}