在链表上交换到前面

时间:2014-12-15 03:31:17

标签: java linked-list

我正在尝试将节点添加到链表中,这样如果包含与新节点相同信息的节点已经存在,它将移动到列表的前面并递增其计数器。但是,当我尝试这个时,我得到的列表比预期的要短。此外,我保留的节点数表明它比它应该更多。我已多次查看代码,无法找到错误。我几乎100%确定错误在add方法中。我有另一个列表使用不同的添加方法(交换列表上方的一个节点)但相同的查找方法,它工作正常。我创建的列表应该有561个节点,但是,它打印出361个节点,numElements返回862个节点。

public static void moveToFront(File FILE_NAME) throws FileNotFoundException
{
    Scanner input = new Scanner(FILE_NAME);                 //Scanner to read the words from the file

    if(FILE_NAME.exists())                                  //Make sure the file is legitimate
    {
        SwapToFirstList List3 = new SwapToFirstList();

        long startTime = System.currentTimeMillis();

        while(input.hasNext())                          //While there are more words
        {
            //System.out.println(input.hasNext());
            String nextWord = input.next().replaceAll("[^a-zA-Z ]", "").toLowerCase();  //Remove all non-letters and convert
                                                                                        //letters to their lowercase form

                                                                                //If the word is already on the list        
            List3.add(nextWord);                                                //swap the node to the front of the list
                                                                                //If the word isn't on the list
                                                                                //add the word to the list
            //System.out.println(nextWord);
        }       

        long elapsedTime = System.currentTimeMillis() - startTime;

        input.close();                                                          //Close the scanner
        System.out.println("List Three Words: " + List3.totalCount());          //Print number of words on the list
        System.out.println("List Three Nodes: " + List3.size());                //Print number of nodes on the list
        System.out.println("List Three Compares: " + List3.comparisons());      //Print out the number of comparisons for the list
        System.out.println("List Three Ref Changes: " + List3.refChanges());    //Print out the number of reference changes
        System.out.println(List3.toString());                                   //Print out the unsorted list
        System.out.println("List Three Time: " + elapsedTime / 1000.0 + " seconds.");
    }

    else                                            //File does not exist
        System.out.println("File Not Found");
}

public void add(T element)
//moves the node to the front of the list
{
    find(element);
    if(!found)
    //element is not on the list
    {
        Node<T> newNode = new Node<T>(element);
        totalCount++;
        newNode.addCount();
        newNode.setLink(list);
        list = newNode;
        refChanges++;
        numElements++;
    }
    else
    //element is already on the list
    {
        if(location == list)
        //element is at the front of the list
        {
            getNode(element).addCount();
            totalCount++;
        }
        else
        //element is not at the front of the list
        {
            previous.setLink(location.getLink());
            location.setLink(list.getLink());
            location.addCount();
            totalCount++;
            list = location;
        }
    }
}

protected void find(T target)
//searches the list for the target
{
    location = list;
    previous = null;
    refChanges++;
    found = false;

    while(location != null)
    //while the list is not empty
    {
        numComparisons++;
        if(location.getInfo().equals(target))
        //if the item matches what we are searching for
        {
            found = true;
            return;
        }
        else
        //go to the next node
        {
            preprevious = previous;
            previous = location;
            location = location.getLink();
            refChanges += 3;
        }
    }
}

public void setLink(Node<T> link)
//set the link for this node
{
    this.link = link;
}

public Node<T> getLink()
//return the link of this node
{
    return link;
}

1 个答案:

答案 0 :(得分:0)

我认为location.setLink(list.getLink())行是问题。

如果list是列表中的第一个元素,则list.getLink()是第二个元素。因此,您要将location之后的下一个元素设置为列表中的第二个元素。这反过来意味着您使用location 替换 列表中的第一个节点,而不是 预先 location到列表中。

要解决此问题,请将以上行替换为:

location.setLink(list);