开发者

Inputting object in correct position of Linked List

开发者 https://www.devze.com 2023-02-17 00:15 出处:网络
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, putti

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.

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...).

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

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


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

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
  • 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
0

精彩评论

暂无评论...
验证码 换一张
取 消