使用列表节点问题中的数据从链接列表中提取值

时间:2015-05-14 10:23:40

标签: java

所以我尝试做的是根据LL中的值从链表中提取某些节点。 (列表类称为WordList) 列表中的节点称为“Word'并包含字段"字符串字"," int score"和" Word next"

运行一个循环,提取具有相同分数的每个Word,然后将它们保存到临时列表中,以便进一步处理:

private WordList sortWords(WordList words)
{
      int maxScore = words.getMaxScore(); //returns the maximum score among words int list
      WordList sortedList = new WordList(); //Initializes new WordList with head = null

      WordList temp; //create a new wordList without initializing it.
      Word check; //create a word to iterate through the list
      for (int i=0; i<=maxScore; i++)
      {
           temp = new WordList();
           check = words.head;
           while (check != null)
           {
                  if (check.score == i)
                      temp.addToTail(check);
                  check = check.next;
           }
           //further prcessing
           temp.sortAlphabetically();
           while(!temp.isEmpty())
                sortedList.addToTail(temp.removeFromHead());
       }
}

正在检查的列表如下:( word:score)

january : 14
february : 13
march : 8
april : 8
may : 9
june : 8
july : 11
august : 9
september : 14
october : 12
november : 13
december : 13

我已经跟踪了这个问题,因为我每次迭代都会打印出temp。在每次迭代时打印出check.score和i,表明它肯定会按预期检查正确的值。而第一个temp的输出预计将于3月4月3月发布。但是,当打印出temp时,我会得到以下列表:

第一次迭代(i = 8)

march : 8
april : 8
june : 8
july : 11
august : 9
september : 14
october : 12
november : 13
december : 13

//expected
//march : 8
//april : 8
//june : 8

第二次迭代:(i = 9)

august : 9
september : 14
october : 12
november : 13
december : 13

//expected:
//may : 9
//august : 9

第三次迭代:(i = 11)

july : 11
august : 9
september : 14
october : 12
november : 13
december : 13

//expected:
//july : 11

第四次迭代(i = 12)

october : 12
november : 13
december : 13

//expected:
//october : 12

第五次迭代(i = 13)

february : 13
november : 13
decmeber : 13

//as expected

最后一次迭代(i = 14)

january : 14
february : 13
november : 13
december : 13

//expected:
//january : 14
//september : 14

如果有人知道发生了什么,请告诉我。

为了完整起见:

//WordList Constructor:
public WordList()
{
     this.head = null;
     this.tail = null;
}

//WordList addToTail function:
public void addToTail(Word node)
{
     if (isEmpty())
          head = tail = node;
     else
     {
          tail.next = node;
          tail = node;
      }
}

//WordList removeFromHead funtion
public word removeFromHead()
{
      if (isEmpty())
           return null;
      else
      {
           Word word = head;
           head = head.next;
           return word;
      }
}

/*The sortAlphabetically function not shown but returns expected results*/

2 个答案:

答案 0 :(得分:0)

我希望您的addToTail(check)调用正在改变check中的指针值。在更改现有对象中的任何指针之前,您需要捕获列表中下一个元素的位置。

另外:对这种设计的简化是将这些东西的来源视为一个队列,例如: while (check = list.removeFromHead()) (注意:分配,不等于)或类似的东西。由于您将事物从一个列表移动到另一个列表,因此源列表有效地被消费,并且#34;所以list.head指针总是指向你需要抓取的下一个元素(如果有的话)。这种观察可以让你修改check而不受惩罚,因为你不需要依赖它的.next指针。

答案 1 :(得分:0)

好的问题是我将节点从原始列表传递到临时列表及其指针。 因此,如果我将检查传递给temp,它会跟随检查列表的其余部分,因为指针仍然很明确。

所以现在正确运行的方法如下:

private WordList sortWords(WordList words)
{
      int maxScore = words.getMaxScore();
      WordList sortedList = new WordList();

      WordList temp;
      Word check;
      for (int i=0; i<=maxScore; i++)
      {
            temp = new WordList();
            check = words.head;
            while(check != null)
            {
                 if (check.score == i)
                 {
                      Word n = new Word(check.word, check.score);
                      temp.addToTail(n);
                 }
            }

            temp.sortAlphabetically();
            while(!temp.isEmpty())
                 soretedList.addToTail(temp.removeFromHead();
       }
}

用于单词的构造函数如下:

public Word(String _word, int _score)
{
      this.word = _word;
      this.score = _score;
      this.next = null;
}

感谢@Mike Robinson,你在这个具体的案例中回答并不是很正确,但你说的话让我意识到我的错误是什么,而你所说的一般是正确我需要偏离一般,因为这个函数是在使用Cichelli方法进行散列之前对单词进行排序。

相关问题