简体   繁体   English

如何在Java中按字母顺序将节点添加到单链接列表中?

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

This is an assignment question for my Intermediate Java class. 这是我的中级Java类的作业问题。 I'm supposed to make a "Dictionary" using linked lists without using the built-in add methods. 我应该使用链接列表来制作“字典”,而不使用内置的add方法。 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). 该程序应该能够以字母升序和降序显示单词列表(及其含义)(两个单独的JOptionPane菜单选项)。 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. 我确实知道,while循环下的if语句出了点问题。 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. 您的while循环跳到链接列表的末尾。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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