简体   繁体   English

按字母顺序的字符串插入Java链接列表

[英]Alphabetical String Insertion Java Linked List

EDIT: Sorry, here is the code where I call the insert() method. 编辑:抱歉,这是我调用insert()方法的代码。 The infinite loop is gone now, but the last case in the insert (else...) still doesn't add a String in order. 无限循环现在消失了,但是插入(else ...)中的最后一种情况仍然没有按顺序添加String。

public static void main(String[] args)
    {
        Frequency f = new Frequency();
        String s1 = "apple";
        String s2 = "crayon";
        String s3 = "bear";
        String s4 = "apple";
        String s5 = "";
        f.insert(s1);
        f.insert(s2);
        f.insert(s3);
        f.insert(s4);
        f.insert(s5);
        f.print();
    }

Output of test: (apple, 2)(crayon, 1) 测试结果:(苹果2)(蜡笔1)


I received a project in my college course which requires me to insert Strings into a Linked List in alphabetical order. 我在大学课程中收到一个项目,该项目要求我按字母顺序将字符串插入“链接列表”。 I think I have the correct method, but running the program causes an infinite loop; 我想我有正确的方法,但是运行程序会导致无限循环; I'm not sure why. 我不知道为什么。 Here is the code I have so far, I would prefer an explanation over code: 这是我到目前为止的代码,我宁愿解释而不是代码:

/*
 * Inserts a word into the linked list. If the word exists, increment the 
 * count by q. 
 */
public void insert(E word){
    //word is empty
    if(word.equals("")){
        return;
    }
    //list is empty
    else if(isEmpty())
    {
        Node n = new Node(word);
        first = n;
        N++;
    }
    //list already has word
    else if(contains(word))
    {
        Node cur = first;
        while(cur != null)
        {
            if(cur.key == word)
                cur.count++;
            cur = cur.next;
        }
    }
    //list only has 1 object
    else if(N == 1)
    {
        Node n = new Node(word);
        first.next = n;
        N++;
    }
    //inserting new object
    else
    {
        Node n = new Node(word);
        Node cur = first;
        while(cur != null)
        {
            if(cur.next != null)
            {
                if(cur.next.key.compareTo(word) <= 0)
                {
                    Node temp = cur.next;
                    cur.next = n;
                    n.next = cur.next;
                    return; //exit, since the word has been added
                }
                cur = cur.next;
            }
            else
                cur.next = n;
        }
        N++;
    }
}

As I see you want to get the count of Keys and all Keys in the right order you may use a 如我所见,您想按正确的顺序获取键和所有键的数量,您可以使用

TreeMap<String,Integer>

with for your purpose and insert by : 用于您的目的,并插入:

treeMap.put(key, treeMap.getOrDefault(key, 0));

To your question: If your are in the last case of your question "cur" can never get null. 对于您的问题:如果您在问题的最后一种情况下,“ cur”永远不会为空。 Because you alter the cur variable only if cur.next != null. 因为仅当cur.next!= null时才更改cur变量。 So once cur.next is null you expect the while would break after the next round. 因此,一旦cur.next为null,您就会希望下一轮之后会中断。 However you don't alter cur's value anymore. 但是,您不再改变cur的价值了。

    while(cur != null)
    {
        if(cur.next != null)
        {
            if(cur.next.key.compareTo(word) <= 0)
            {
                Node temp = cur.next;
                cur.next = n;
                n.next = cur.next;
                return; //exit, since the word has been added
            }
            ---> old position of cur = cur.next;
        }
        else {
            cur.next = n;
        }
        cur = cur.next; ---> move it here
    }

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

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