Java 找到正确的位置并存储在LinkedList中

Java 找到正确的位置并存储在LinkedList中,java,sorting,linked-list,Java,Sorting,Linked List,我正在尝试将字符串存储在LinkedList中。我不允许预先排序,但可以找到位置并将字符串传递给链接列表。当我通过文本文件传递字符串时,字符串不会通过最后一个else条件。 我的输入文件已被删除 乔 阿皮 阿普斯 斑马 猫 当它到达appz时,它不会通过任何语句。它应该插入最后一个else条件并打印5,但不这样做 /** * Gets the string and arranges them in order * @param newString */ pu

我正在尝试将字符串存储在LinkedList中。我不允许预先排序,但可以找到位置并将字符串传递给链接列表。当我通过文本文件传递字符串时,字符串不会通过最后一个else条件。 我的输入文件已被删除 乔 阿皮 阿普斯 斑马 猫

当它到达appz时,它不会通过任何语句。它应该插入最后一个else条件并打印5,但不这样做

/**
     * Gets the string and arranges them in order
     * @param newString
     */
    public void store(String newString) {

        LinkedListNode current = head;
        System.out.println(newString);

        // if no element in the list
        if (current==null){
            System.out.println("1");
            makeNode(newString);
        }

        // if only 1 elements in the list
        else if(current.getNext()==null ){
            System.out.println("2");
            if(newString.compareTo(current.getName())<0){
                insertBefore(current.getName(),newString);
            } else{
                insertAfter(current.getName(),newString);
            }
        }

        // if the element is smaller than the head in the list
        else if(newString.compareTo(current.getName()) < 0){
            System.out.println("3");
            LinkedListNode temp = makeNode(newString);
            temp.setNext(current);
            head=temp;
        }

        // if the element is greater than the tail in the list
        else if(newString.compareTo(findTail().getName()) > 0){
            System.out.println("4");
            insertAfter(findTail().getName(),newString);
        }

        // for more than two elements in the list
        else{
            System.out.println("5");
            while(!(newString.compareTo(current.getName())>0  && newString.compareTo(current.getNext().getName())<0 ) && current.getNext()!=null){
                current=current.getNext();
            }

            if(newString.compareTo(current.getName())<0){
                insertBefore(current.getName(),newString);
            }
            else{
                insertAfter(current.getName(),newString);
            }
        }

    }   // end of store()
/**
*获取字符串并按顺序排列
*@param新闻字符串
*/
公共无效存储(字符串新闻字符串){
LinkedListNode当前=头;
System.out.println(新闻字符串);
//如果列表中没有元素
如果(当前==null){
系统输出打印项次(“1”);
makeNode(新闻字符串);
}
//如果列表中只有1个元素
else if(current.getNext()==null){
系统输出打印项次(“2”);
if(newString.compareTo(current.getName())0){
系统输出打印项次(“4”);
insertAfter(findTail().getName(),newString);
}
//对于列表中的两个以上元素
否则{
系统输出打印项次(“5”);

当(!(newString.compareTo(current.getName())>0和&newString.compareTo(current.getNext().getName())进行遍历时,不应更改头引用。要进行遍历,只需执行以下操作:

Node tmp = head;
while(tmp != null) tmp = tmp.next;
这将非常方便地确定在何处插入新节点或在何处删除现有节点

您的类还应该具有addFirst、addLast、insertBefore、insertAfter的方法


你以前的插件有问题。我更新了它

public void insertBefore(String later, String name){
        if(head==null){
            head = new LinkedListNode(name,null);
        }
        else if(head.getName()==later){
            LinkedListNode newNode = makeNode(name);
            newNode.setNext(head);
            head=newNode;   
        }
        else{
            LinkedListNode current = head;
            while(current.getNext().getName()!=later){
                current=current.getNext();
            }
            LinkedListNode newNode = makeNode(name); // create the new node
            newNode.setNext(current.getNext());    
            current.setNext(newNode);
        }
    } // end of insertBefore()

是否只有appz有这个问题,或者是appz和更高版本的所有问题?如果之前的条件都没有执行,那么最后一个else条件应该执行。所以不管怎样,你看它,它应该打印出一个数字-正确吗?@Sunil:ya suil,我至少应该转到最后一个语句,但在打印5之前就停止了,但是程序ram继续运行。冻结在appz.Hmm的位置。很有趣。因为我没有所有的代码,我需要你在每一行中放置断点,从第一个if(带有if的行(current==null))到最后一个else(打印5的行上方的行).调试程序,你能告诉我程序在哪一行停止吗?如果一切正常,它应该一直到最后一行,但显然没有发生。告诉我哪一行,或者你能把所有代码转储到某个地方,我可以看一下it@Sunil:尝试过调试,但未完成最后一部分。提供了我正在给你链接到类文件。谢谢,它成功了!我忘了创建新节点,而是更改了头。
if (current==null){
    System.out.println("1");
    makeNode(newString);
}

// if only 1 elements in the list
else if(current.getNext()==null ){
    System.out.println("2");
    if(newString.compareTo(current.getName())<0){
        insertBefore(current.getName(),newString);
    } else{
        insertAfter(current.getName(),newString);
    }
}

// if the list has more than one element
else
{
    // figure out where it goes (before or after) and insert
}
if (current==null){
    System.out.println("1");
    makeNode(newString);
}

// if the list has one or more elements
else
{
    // figure out where it goes (before or after) and insert
}
public void insertBefore(String later, String name){
        if(head==null){
            head = new LinkedListNode(name,null);
        }
        else if(head.getName()==later){
            LinkedListNode newNode = makeNode(name);
            newNode.setNext(head);
            head=newNode;   
        }
        else{
            LinkedListNode current = head;
            while(current.getNext().getName()!=later){
                current=current.getNext();
            }
            LinkedListNode newNode = makeNode(name); // create the new node
            newNode.setNext(current.getNext());    
            current.setNext(newNode);
        }
    } // end of insertBefore()