Abhishek Sharma
Abhishek Sharma

Reputation: 107

Insertion in sorted doubly linked list

I am given the pointer to the head node of a sorted doubly linked list and an integer to insert into the list.I am told to create a node and insert it into the appropriate position in the list such that its sorted order is maintained. The head node might be NULL.

Sample Input

NULL , data = 2

NULL <-- 2 <--> 4 <--> 6 --> NULL , data = 5

Sample Output

NULL <-- 2 --> NULL

NULL <-- 2 <--> 4 <--> 5 <--> 6 --> NULL

I tried the above problem.But My program is terminating due to timeout.What am I doing wrong in the below code. Assume Node class and main function is already there. Many Thanks in advance!!

Node SortedInsert(Node head,int data) {

    Node newn = new Node();

    newn.data = data;
    newn.prev=null;
    newn.next = null;

    Node ptr = head;
    Node nex=head.next;

    while(ptr!=null && nex!=null) {
        if(ptr.data<=newn.data && nex.data>=newn.data) {
            newn.next = nex;
            newn.prev = ptr;
            nex.prev = newn;
            ptr.next = newn;
        }            
        else {
            nex=nex.next;
            ptr=ptr.next;
        }
    }

    if(ptr!=null && nex==null) {
        if(ptr.data>=newn.data) {
            newn.next=ptr;
            ptr.prev=newn;
            newn.prev=null;
            head=newn;
        }
        else {
            ptr.next=newn;
            newn.prev = head;
        }
    }

    if(head==null) {
        head = newn; 
    }

    return head;

}

Upvotes: 0

Views: 3278

Answers (2)

Pablo Lozano
Pablo Lozano

Reputation: 10342

If the head node is null you'll geta NullPointerException while trying to get the next/prev nodes. You should check that first:

Node sortedInsert(Node head, int data) {
    Node newn = new Node();
    newn.data = data;
    //Basic case: the list is empty, so the head is null
    if (head==null) {
        return newn;
    }
    //If node is not null
    Node aux= head;
    Node auxPrev;
    while (aux!=null && aux.data<data) {
        auxPrev=aux;
        aux=aux.next;
    }
    //auxPrev will be null if we are going to insert in the first position
    if (auxPrev!=null)
        auxPrev.next=newn;
        newn.prev=auxPrev;
        head=newn;
    }
    //aux will be null if we insert in the last position
    if (aux!=null) {
        aux.prev=newn;
        newn.next=aux;
    }
    return head;
}

Upvotes: 2

Syntac
Syntac

Reputation: 1767

Fairly simple: You are not breaking out of the loop after succesfully inserting. Therefore it keeps looping over the position it inserts the node in. Make a tiny change:

if(ptr.data>=newn.data)
{
    newn.next=ptr;
    ptr.prev=newn;
    newn.prev=null;
    head=newn;
    break;
}

However, you have some redundant code written. This is shorter and doesn't contain redundant code:

Node SortedInsert(Node head,int data) {

    Node newn = new Node();
    newn.data = data;  

    Node ptr = head;

    if (ptr == null) {
        head = newn;

    } else if ( ptr.data > newn.data ) {
        newn.next = ptr;
        ptr.prev = newn;
        head = newn;

    } else {
        Node nex = head.next;

        while (nex != null && nex.data <= newn.data) {
            ptr = nex;
            nex = nex.next;
        }

        ptr.next = newn;
        newn.prev = ptr;

        if (nex != null) {
            nex.prev = newn;
            newn.next = nex;
        }
    }

    return head;   
}

Upvotes: 2

Related Questions