简体   繁体   中英

How do I add nodes to a singly linked list alphabetically in java?

This is an assignment question for my Intermediate Java class. I'm supposed to make a "Dictionary" using linked lists without using the built-in add methods. I'm having trouble getting it to add the nodes alphabetically. I have no problems just appending or prepending each node to the list, but that's not what the question is asking for. The program is supposed to be able to display the list of words (and their meanings) in both ascending and descending alphabetical order (two separate JOptionPane menu options). Its proving...difficult. Here's my add method:

void add(WordMeaning wm)
{
    WordMeaningNode word = new WordMeaningNode(wm);
    WordMeaningNode current, prev;

    try
    {
        if(wmn == null)
        {
            wmn = word;
        }
        else
        {
            current = wmn;
            prev = null;
            while(current.next != null)
            {
                prev = current;
                current = current.next;
            }
            if(word.wm.getWord().compareToIgnoreCase(current.wm.getWord()) < 0)
            {
                word.next = current.next;
                current.next = word;
            }
            else if(word.wm.getWord().compareToIgnoreCase(current.wm.getWord()) > 0)
            {
                prev = current;
                current.next = word;
            }
        }
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }
}

I do know for certain that something is wrong with the if-statement under the while loop. I just have no idea what. I'm not good enough yet to know what's wrong(lol). I'm wondering if I'm supposed to just append them normally first, THEN take that and sort it alphabetically. If so then how would I do that? Please be gentle. ;)

I think that you can first add all the words in ascending order and then can iterate over them in the sequence required (ascending / descending). Since you already have double linked list with previous and next nodes iteration should not be an issue. Try :

while(current.next != null)
        {
            prev = current;
            current = current.next;

            if(word.wm.getWord().compareToIgnoreCase(current.wm.getWord()) > 0)
            {
                word.next = current.next;
                current.next = word;
                break;  // break when correct position of word is found in ascending order.
            }
          /*  else if(word.wm.getWord().compareToIgnoreCase(current.wm.getWord()) > 0)
            {
                prev = current;
                current.next = word;
            }
          */  // No else condition is required
        }
}

Your while loop skips to the end of the linked list. At this end you try to determine where to insert or append the node (the if you mention.)

What you want to do is to skip to the insert point (find the node that is followed by a node that is bigger than the node to insert.)

You need something like (pseudo code as this is an assignment):

boolean found = false;
while (!found && current.next != null) {
    if (next word > word) {
        found = true;
    } else {
         prev = current;
         current = current.next;
    }
}
if (!found) {
    append to end of list
} else {
    insert before next
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM