Better method structure for insertion in doubly linked list Here is my insertion method for a doubly linked list. It works, but passing two parameters for the method seems cumbersome. Can anyone suggest a cleaner approach than this?

   LinkedList list = new LinkedList();
    Link link1 = new Link("A");
    list.insert(list.head,link1);
    Link link2 = new Link("B");
    list.insert(link1, link2);
    Link link3 = new Link("C");
    list.insert(link2, link3);
    list.display_List_Start_From_Head();

public void insert(Link currentNode, Link newNode)
    {
        if(head == null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            currentNode.next = newNode;
            newNode.prev = currentNode;
            tail = newNode;
        }


    }
if(head == null)
    {
        head = newNode;
        tail = newNode;
    }
    else
    {
        currentNode.next = newNode;
        newNode.prev = currentNode;
        tail = newNode;
    }


hmmm

    if(head == null)
    {
        head = newNode;
    }
    else
    {
        currentNode.next = newNode;
        newNode.prev = currentNode;
    }
    tail = newNode;


Yep, that's shorter.



Okay, and now a more serious answer:

A LinkedList, no matter if it's doubly linked or not, always is gonna require two things for insertAt: The object to insert, and some means of specifying where it goes. There's no way to circumvent this. Best you can do is take a Node (or Link, in your case) and give it an insertAfter method.



Oh yeah, back to your code example:

LinkedList list = new LinkedList();
Link link1 = new Link("A");
list.insert(list.head,link1);
Link link2 = new Link("B");
Link link3 = new Link("C");
list.insert(link2, link3);
list.insert(link1, link2);
list.display_List_Start_From_Head();


You have some serious bugs in there.

This should work fine, of course, but try iterating it backwards; you won't get the correct elements back.

Here's another way your list blows up:

LinkedList list = new LinkedList();
Link link1 = new Link("A");
list.insert(list.head,link1);
Link link2 = new Link("B");
list.insert(link1, null);//NullPointerException
list.insert(link1, link2);//If it didn't crash just now, this will clear the list and leave link2 as head