在链接列表的正确位置输入对象

时间:2022-02-17 07:17:37

I have a csv file with a few rows (group #, # of elements in group, element #) and I need to place these inside of a Linked List. I have this happening while the csv is being read in by the file, putting it into the tmpPacket object, then placing the tmpPackets into the nodeList (linked list) and am trying to have it adding to the Linked List in order so if the group # is the same as a previous one, it adds it to the beginning of that group, otherwise to the end of the linked list.

我有一个包含几行的csv文件(组#,组中的元素数,元素#),我需要将它们放在链接列表中。我正在发生这种情况,同时csv被文件读入,将其放入tmpPacket对象,然后将tmpPackets放入nodeList(链表)并尝试将其添加到链接列表中,以便如果该组#与前一个相同,它将其添加到该组的开头,否则添加到链接列表的末尾。

Anyways, I have it so far working to the point where it will add one group # to the Linked List, but ignores the rest of the groups. example input would be:

无论如何,到目前为止,我已经将它添加到链接列表中,但忽略了其余的组。示例输入将是:

4,3,2
5,1,1
4,3,1
4,3,3
2,2,2
3,1,1
2,2,1

and basically I want it so when it is added to the linked list it will look like:

基本上我想要它,所以当它被添加到链表时,它看起来像:

4,3,1
4,3,2
4,3,3
5,1,1
2,2,1
2,2,2
3,1,1

(the exact order doesn't matter. 4, 5, 2, and 3 can be in any order, important is that the 4's are together, 5's are together...).

(确切的顺序无关紧要.4,5,2和3可以是任何顺序,重要的是4是在一起,5是在一起......)。

Here is what I have that is only outputting the 4's and nothing else.

这就是我所拥有的只输出4而不是其他的东西。

int currLength = nodeList.getLength();
        int finishNum = 0;
        for(int tmpGo=1;tmpGo<=currLength;tmpGo++){
            if(finishNum == 0){
                int itr = 0;
                int addEnd = 0;
                while(itr<nodeList.getLength()){
                    itr++;
                    if(nodeList.getEntry(itr).getPageID() == pageID) {
                        nodeList.add(tmpGo, tmpPacket);
                        finishNum = 1;
                        addEnd = 1;
                        break;
                    } 
                }


            } else {
                break;
            }
        }

1 个解决方案

#1


0  

So, I don't know what your nodeList is, but according to your initial description you would need this:

所以,我不知道你的nodeList是什么,但根据你的初始描述你需要这个:

int i;
int l = list.length();
for (i = 0; i < l; i++)
    if (list.getEntry(i).key() == newKey)
        break;
list.insert(newEntry, i);

This example assumes:

此示例假定:

  • List entries are numbered from 0 until length - 1
  • 列表条目的编号从0到length - 1

  • Inserting an entry at the length is the same as appending it
  • 以长度插入条目与附加条目相同

However, it will not result in your sample result. Instead you will get:

但是,它不会导致您的样本结果。相反,你会得到:

4,3,3
4,3,1
4,3,2
5,1,1
2,2,1
2,2,2
3,1,1

#1


0  

So, I don't know what your nodeList is, but according to your initial description you would need this:

所以,我不知道你的nodeList是什么,但根据你的初始描述你需要这个:

int i;
int l = list.length();
for (i = 0; i < l; i++)
    if (list.getEntry(i).key() == newKey)
        break;
list.insert(newEntry, i);

This example assumes:

此示例假定:

  • List entries are numbered from 0 until length - 1
  • 列表条目的编号从0到length - 1

  • Inserting an entry at the length is the same as appending it
  • 以长度插入条目与附加条目相同

However, it will not result in your sample result. Instead you will get:

但是,它不会导致您的样本结果。相反,你会得到:

4,3,3
4,3,1
4,3,2
5,1,1
2,2,1
2,2,2
3,1,1