CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    2

    incremented loop question

    -Hello, I am trying to loop through a linked list so that after all elements are looped through, the first element
    -is removed from the list, and then after the new loop occurs again, the second item is removed, this occurs until the second to
    -last node and last node remain.

    Example print out:

    loop 1: A,B,C,D,E
    loop 2: B,C,D,E
    loop 3: C,D,E
    loop 4: D,E
    loop 5: E



    Here is my code (wanting it to do above increment in tree3Loop method.

    public void tree3Loop(){
    for(int k=0;k<clusterList3.size();k++)
    {
    buildTree3();
    System.out.println(k);

    }

    }

    public void buildTree3() {
    // if no common elements, then new node is set as parent and shifted x_increment to right
    // if not parent, then attach to node that shares common element(s)
    // ListIterator itr3 = clusterList3.listIterator();

    //Loop through all pairwise combinations


    // ClusterNode's first element is the root node
    for (int i=1; i<clusterList3.size(); i++) { // start from 2nd node
    ClusterNode2 cNode3 = clusterList3.get(i); // current node
    for (int j=0; j<i && cNode3.isRoot(); j++) {
    ClusterNode2 pNode3 = clusterList3.get(j); // prev node already has (x,y) fixed
    LinkedList<String> commonCDTs3 = cNode3.getCommonCDTs(pNode3);
    if (commonCDTs3.size() != 0) {
    cNode3.setParent(pNode3); // cNode is child to pNode
    pNode3.addChild(cNode3);
    }
    }
    if (cNode3.isRoot()) { // no common elements with any nodes already in list
    rootClusterList3.add(cNode3);
    cNode3.setLoc(3, x_marker3, y_marker3);
    moveMarkers3(); // shift x marker to next parent
    }
    }


    // Now all root nodes have their location fixed so we run BFS to fix children location
    for (int i=0; i<rootClusterList3.size(); i++) {
    queue3.add(rootClusterList3.get(i));
    BFS3();
    }



    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: incremented loop question

    Please:
    • Post code in Code Tags
    • Ask a question - if you don't ask a question we don't know what level of want.
    • Tell us what is wrong - if you don't tell us what is wrong we can't help you.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jul 2010
    Posts
    2

    Re: incremented loop question

    Ok, how do I run an incremented loop from tree3Loop where the linked list (clusterList3) output of nodes is

    0,1,2,3,4
    1,2,3,4
    2,3,4
    3,4
    4

    ? Currently I am trying after each increment to remove the top node and run the loop again (and my buildTree3 method) so that each time the number of nodes is smaller and smaller. The best I can do is

    0,1,2,3,4 and I am trying remove but it doesn't increment back into my loop tree3Loop() method.


    Code:
    public void tree3Loop(){
         for(int k=0;k<clusterList3.size();k++)
        {
          buildTree3();
          System.out.println(k);
          clusterList3.remove(k);
        }
    
       }
    
    public void buildTree3() {
    
    
    
    //Loop through all pairwise combinations
    
    
    // ClusterNode's first element is the root node
           for (int i=1; i<clusterList3.size(); i++) { // start from 2nd node
            ClusterNode2 cNode3 = clusterList3.get(i); // current node
                  
                  for (int j=0; j<i && cNode3.isRoot(); j++) {
                    ClusterNode2 pNode3 = clusterList3.get(j); // prev node already has (x,y) fixed
                     LinkedList<String> commonCDTs3 = cNode3.getCommonCDTs(pNode3);
                           
                        if (commonCDTs3.size() != 0) {
                              cNode3.setParent(pNode3); // cNode is child to pNode
                              pNode3.addChild(cNode3);
                         }
                 }
              
                   if (cNode3.isRoot()) { // no common elements with any nodes already in list
                    rootClusterList3.add(cNode3);
                    cNode3.setLoc(3, x_marker3, y_marker3);
                    moveMarkers3(); // shift x marker to next parent
                   }
    }
    
    
          // Now all root nodes have their location fixed so we run BFS to fix children location
            for (int i=0; i<rootClusterList3.size(); i++) {
            queue3.add(rootClusterList3.get(i));
            BFS3();
           }
    
    
    
    }

  4. #4
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: incremented loop question

    Well, if I was doing this, I'd first loop through all the nodes in the list. Then I'd use the node in the list from that loop as the starting point in a second loop. In the second loop I'd print the list from the selected node to the end.

    Something like this:
    Code:
    for(int i = 0; i < list_size; i++)
    {
        for(int j = i; j < list_size; j++)
        {
            // print all list nodes indexed by j
        }
    }
    Of course, you'd have to adjust this if you really have to delete a node each time through.

    BTW, it would help if you show us all your code when you post.

  5. #5
    Join Date
    Jul 2010
    Posts
    17

    Re: incremented loop question

    In your method
    Code:
    public void tree3Loop(){
         for(int k=0;k<clusterList3.size();k++)
        {
          buildTree3();
          System.out.println(k);
          clusterList3.remove(k);
        }
    
       }
    you are changing the size of your list, and doing a size check on each iteration, are you assuming that will be constant?

    i.e. after the first iteration, clusterList3.size() will be 1 less than whatever it started with. Unless K is greater than this value, you will only do one iteration of you loop.

    A general note for removing things from a list in this manner is to change your loop condition to

    Code:
     for(int k=clusterList3.size(); k>0;k--)
    and to remove the last element each time (index=k-1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured