在链接列表中按字母顺序放置字符节点

时间:2015-04-19 03:11:36

标签: java linked-list character alphabetical

我一直试图弄清楚如何做这一段时间,而我却无法弄清楚我做错了什么。我需要做的是使用字符创建一个链表,并按字母顺序显示列表。

主程序:

public static void main(String[] args) {

    String n = null;
    char newChar = ' ';
    KeyboardReader reader = new KeyboardReader();

    Node start = null;
    Node last = null;
    Node temp = null;

            do{
                System.out.print("Enter a letter: ");
                newChar = reader.readChar();

                temp = new Node(newChar);

                if (start == null)
                     start = temp;
                if (last != null){ 
                    last.nodeptr = temp;
                }
                last = temp;

                System.out.print("Linked list: ");
                printList(start);

                System.out.print("Would you like to enter another letter (y/n)? ");
                n = reader.readLine();
                addLine();

            }while(n.compareTo("y") == 0);

            System.out.println("The following will output a linked list created in alphabetical order.");
            addLine();

            start = null;
            last = null;
            temp = null;

            do{
                System.out.print("Enter a letter: ");
                newChar = reader.readChar();

                temp = new Node(newChar);

                if(start == null){
                    start = temp;
                    last = temp;
                }

                //~~ CAUSING ISSUES ~~
                //if(last != null)
                    //last.nodeptr = temp;


                //insert before
                if(temp.letter < start.letter){
                    temp.nodeptr = start;
                    start = temp;   
                }

                //insert at middle or end
                else{
                    if(last.letter < temp.letter){
                        last.nodeptr = temp;
                        temp = last;
                    }
                    if(last.letter > temp.letter){
                        temp.nodeptr = last;
                        last = temp;
                    }
                }

                //For testing purposes
                /*else{
                    if(temp.letter > start.letter){
                        if(start.nodeptr != null){
                            while(temp.letter > start.nodeptr.letter){
                                temp.nodeptr = start;
                                start = temp;
                            }
                        }
                    }
                    else if(temp.letter < last.letter){
                        if(last.nodeptr != null){
                            while(temp.letter < last.nodeptr.letter){
                                last.nodeptr = temp;
                                last = temp;
                            }
                        }
                    }
                }
                */

                System.out.print("Linked list: ");
                printList(start);

                System.out.print("Would you like to enter another letter (y/n)? ");
                n = reader.readLine();
                addLine();

            }while(n.compareTo("y") == 0);
}    

当前输出(与注释掉的东西一样):

Enter a letter: m  
Linked list: m  
Would you like to enter another letter (y/n)? y  

Enter a letter: o  
Linked list: mo  
Would you like to enter another letter (y/n)? y  

Enter a letter: n  
Linked list: mon  
Would you like to enter another letter (y/n)? y  

Enter a letter: e   
Linked list: mone  
Would you like to enter another letter (y/n)? y  

Enter a letter: y  
Linked list: money  
Would you like to enter another letter (y/n)? n  

The following will output a linked list created in alphabetical order.  

Enter a letter: m  
Linked list: m  
Would you like to enter another letter (y/n)? y  

Enter a letter: o  
Linked list: mo  
Would you like to enter another letter (y/n)? y  

Enter a letter: n  
Linked list: mn  
Would you like to enter another letter (y/n)? n     

我想出了程序的第一部分,它只是作为用户输入插入每个字符。但是,现在我必须按字母顺序排列,我认为我确定了我的问题,我认为这是由于Last节点。我在程序中对它进行了评论并运行,但不正确。如果我把它留下未注释,它会循环并基本上打破程序。

如果有人可以帮我修复代码并使其100%正常工作,我们将不胜感激!

1 个答案:

答案 0 :(得分:2)

当您在列表中添加新字符时,基本上需要考虑四种情况:

  1. 如果列表为空,则新字符将成为列表的头部和尾部。
  2. 如果新字符位于列表头部的节点之前,则它将成为列表的头部并指向列表的旧头部。
  3. 如果新字符出现在列表尾部的节点之后,则它将成为列表的尾部,旧的尾节点指向它。
  4. 如果这些都不是真的,那么节点就会出现在列表中间的某个位置。
  5. 案例1和2看起来正确,因为你有:

    if(start == null){
        start = temp;
        last = temp;
    }
    
    //insert before
    if(temp.letter < start.letter){
        temp.nodeptr = start;
        start = temp;   
    }
    

    案例3有一个小错误:

    if(last.letter < temp.letter){
        last.nodeptr = temp;
        temp = last;
    }
    

    而不是temp = last,我认为你想要last = temp,因为temp应该取代它作为列表中的最后一个节点。这就是为什么当你输入&#34; N&#34;进入&#34; M&#34;和&#34; O&#34;,&#34; O&#34;已经消失了。

    案例4是您需要做的工作。由于新节点可以在列表中的任何位置,并且列表可以包含任意数量的元素,因此您只能使用一系列if语句来执行此操作。到目前为止已完成。相反,您需要使用循环遍历列表并找到插入新节点的适当位置。这足以让你走了吗?