Search code examples
javaalgorithmsortingdoubly-linked-list

Problem when alphabetically inserting a node into a doubly linked list


Im trying to write an insertion method for a doubly linked list where as i'm placing the node into the list it is being inserted in alphabetical order. This idea is I will traverse through the list with a curnode and if the newNode comes before the curnode, I will simply place the newNode before the curnode. The code I have written so far works for inserting 1 node into the list, but i'm having problems with the second part that requires checking the order and placing before. How should I change the program to make it work properly? With the code I have now only 1 element (head) will be inserted.

void insert(String x){
        node curnode = head;
        node newNode = new node(x);

        //list is empty so insert as normal - [works fine]
        if (head == null){
            head = newNode;
            head.prev = null;
            head.next = null;
            tail = newNode;
        }
        //Now insert node with respect to alphabetical order - [problem area]
        else {
           
            // while the list isn't empty 
            while (curnode != null){

                // if newNode alphabetically comes before the curnode then place before
                if (curnode.data.compareTo(newNode.data) > 0){
                    node temp = curnode.prev;
                    curnode.prev = newNode;
                    newNode.next = curnode;
                    newNode.prev = temp;
                    break;
                }
            }
           
        }
    }

Solution

  • There are a few things missing with your implementation. Compare it with this working solution:

    void insert(String x){
        node curnode = head;
        node lastnode = null;
        node newNode = new node(x);
        
        //list is empty so insert as normal - [works fine]
        if (head == null){
            head = newNode;
            head.prev = null;
            head.next = null;
            tail = newNode;
        }
        //Now insert node with respect to alphabetical order - [problem area]
        else {
            // while the list isn't empty 
            while (curnode != null){
                // if newNode alphabetically comes before the curnode then place before
                if (curnode.data.compareTo(newNode.data) > 0){
                    
                    node temp = curnode.prev;
                    curnode.prev = newNode;
                    newNode.next = curnode;
                    newNode.prev = temp;
                    if(temp != null) {
                        temp.next = newNode;
                    } else {
                        // If newnode gets inserted in the head
                        head = newNode;
                    }
                    break;
                }
                
                lastnode = curnode;
                curnode = curnode.next;
            }
            if (curnode == null) {
                // insert to the last
                lastnode.next = newNode;
                newNode.prev = lastnode;
                tail = newNode;
            }
           
        }
    }