简体   繁体   中英

Loop condition for accessing all of a List's Nodes

for(int i=1;i<list.size();i++) 
{
    if (x.nextNode!=null) 
    {
        if (x.data=='C') 
        {
            x.data='G';
        } else if (x.data=='G') {
            x.data='C';
        } else if (x.data=='A') {
            x.data='T';
        } else if (x.data=='T') {
            x.data='A';
        }       
    } 
    x=x.nextNode;
}

I have created a list with char Nodes which only contains AGCT and a loop which checks every Node of the list and changes it. G should be changed to C, C should be changed to G, A should be changed to T, T should be changed to A.

My problem is that every Node.char item is changed except for the last Node of the list. How I should edit this code to also change the last Node?

You are starting from 1 so apparently skipping one node. so to traverse whole LinkList (seems like) you should to start from 0

for(int i=0;i<list.size();i++)
 //       ^

update : when if (x.nextNode!=null) is false mean you have reached the last node.

if (lastNode.nextNode!=null) will be false so no execution will take place ,so to execute the last node , use

if (x!=null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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