简体   繁体   English

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

[英]Extracting values from linked list using data in list node issue

So what I'm trying to do is extract certain nodes from a linked list based on a value in the LL. 所以我想做的是基于LL中的值从链接列表中提取某些节点。 (The list class is called WordList) The nodes within the list are called 'Word' and contain the fields "String word","int score", and "Word next" (列表类称为WordList)列表中的节点称为“ Word”,其中包含字段“字符串单词”,“整数得分”和“下一个单词”

Running a loop which extracts each Word which has the same score and then saves them to a temp list for further processing as such: 运行一个循环,提取具有相同分数的每个单词,然后将它们保存到临时列表中,以便进一步处理,例如:

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());
       }
}

The list being checked is as follows: (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

I've traced the issue to this point as I'm printing out temp with every iteration. 我在每次迭代中都打印出temp时,已经将问题追溯到了这一点。 Printing out check.score and i at every iteration revealed that it is definitely checking the correct values as expected. 在每次迭代中打印出check.score和i都表明它肯定是按预期检查正确的值。 and the first output for temp is expected to be march, april, june. 预计temp的第一个输出将是3月,4月,6月。 However when printing out temp I get the following lists: 但是,当打印出临时文件时,我得到以下列表:

the first iteration (i=8) 第一次迭代(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

the second iteration: (i=9) 第二次迭代:(i = 9)

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

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

the third iteration: (i = 11) 第三次迭代:(i = 11)

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

//expected:
//july : 11

the fourth iteration (i = 12) 第四次迭代(i = 12)

october : 12
november : 13
december : 13

//expected:
//october : 12

the fifth iteration (i = 13) 第五次迭代(i = 13)

february : 13
november : 13
decmeber : 13

//as expected

final iteration (i = 14) 最终迭代(i = 14)

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

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

If anybody has any idea what's going on please let me know. 如果有人知道发生了什么事,请告诉我。

For the sake of completeness: 为了完整性:

//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*/

I expect that your addToTail(check) call is altering the pointer values within check . 我希望您的addToTail(check)调用正在更改check的指针值。 You need to capture where the next element in the list will be, before changing any of the pointers in the object you now have. 在更改您现在拥有的对象中的任何指针之前,您需要捕获列表中下一个元素的位置。

Also: a simplification to this design would be treating the source of these things as a queue, eg while (check = list.removeFromHead()) (note: assignment, not equal-to) or some such thing like that. 另外:这种设计的简化将把这些东西的来源当作一个队列,例如while (check = list.removeFromHead()) (注意:赋值,不等于)或类似的东西。 Since you're moving things from one list to another, the source list is effectively "being consumed," so the list.head pointer always points to the next element (if any) that you need to grab. 由于您要将事物从一个列表移到另一个列表,因此源列表实际上是“被消耗了”,因此list.head指针始终指向您需要抓取的下一个元素(如果有)。 This observation would let you modify check with impunity, because you need not rely on its .next pointer. 这种观察将使您不受惩罚地修改check ,因为您不必依赖其.next指针。

Ok so the problem was that I was passing the nodes from the original list into the temp list, along with their pointers. 好的,问题是我正在将原始列表中的节点及其指针传递到临时列表中。 So if I was passing check into temp it came along with the remainder of the list that followed check as the pointers were still in tact. 因此,如果我将check传递给temp,那么它将与检查之后的其余列表一起出现,因为指针仍然完好无损。

So the method, now correctly functioning is as follows: 因此,现在可以正常运行的方法如下:

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();
       }
}

The constructor used for word is as follows: 用于word的构造函数如下:

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

Thank you to @Mike Robinson, you're answer wasn't quite correct in this specific case but what you said made me realize what my mistake was, while what you're saying in general is correct I needed to deviate from the general as this function is to sort words before they get hashed using Cichelli's method. 谢谢@迈克·罗宾逊,您的回答在此特定情况下并不完全正确,但是您所说的使我意识到我的错误,而您总体上说的是正确的,所以我需要偏离一般该功能是在使用Cichelli方法对单词进行散列处理之前对单词进行排序。

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

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