Thread: Linked lists; getting the current elements next element

  1. #16
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    and the only reason i put
    Code:
    printf("\n%f %f\n", start->numberone, start->numbertwo);
    inside the if statement, was to specifically debug the formula. If i chuck the above inside where the formula is the last element is skipped that is why it does not equal 183.

    Code:
     printf("\n%f %f\n", start->numberone, start->numbertwo);



    There wouldnt be anything there anyway.
    Yep. You suggested that i put the numberone and numbertwo print outs out of the if statement, which worked but start->next != NULL stops the formula from getting the last element in the linked list.

  2. #17
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Where did you get the final result of 183? Just wondering. If you have 174 before adding the last element. Can you explain how it itll get 183 from statring here(174)??

  3. #18
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Code:
    double test = 0;
          test += sqrt( ( pow( (28.80 - 5.60), 2) + pow( (17.60 - 79.12), 2) ));
            test += sqrt( ( pow( (5.60 - 55.30), 2) + pow( (79.12 -  49.90), 2) ));
          test += sqrt( ( pow( (55.30 -  22.22), 2) + pow( (49.90 -   11.11), 2) ));
    
    = 174.382311
    
    
    ## test += sqrt( ( pow( (22.22 - 28.80), 2) + pow( (11.11 -  17.60), 2) ));
    ## which is the skipped element

    makes it 183.624415

  4. #19
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    I hope this would be the solution

    Code:
    void printList(charNode *start)
    {
       float total;
       float x1, x2;
       charNode *fElement = start;
    
       while (start != NULL )
       {
          x1 = fElement->numberone;
          x2 = fElement->numbertwo;
          
          if (start->next != NULL){
             x1 = start->next->numberone;
             x2 = start->next->numbertwo;
          } 
          
          printf("\n%f %f\n", start->numberone, start->numbertwo);
          total += sqrt( ( pow( (start->numberone - x1), 2) + pow( (start->numbertwo - x2 ), 2) ));
             
          start = start->next;
    
        printf("%s %.2f", "\nTotal is now ", total);
    
       }
    }
    The problem aint the last term being skipped. If you explained that your having a problem with your total it would have been solved earlier. It got really far from the real problem

  5. #20
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yes it does, thanks! I'm just curious however, if the last element in my original code wasn't being skipped then why was nothing being printed when it got to the last element? and it was working if it was placed before the if (start->next != null statement.

  6. #21
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Which original code??

    this one?
    Code:
    while (start != NULL)
    {
        printf("\n%f %f\n", start->numberone, start->numbertwo);
        printf("%s %.2f %.2f %s", "next element is -+> ", start->next->numberone, start->next->numbertwo, "**OK**");
        start = start->next;
    }
    Edited*

    The problem i see with you is that. You probably have a wrong picture of how your list really look like.

  7. #22
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    no this one:

    Code:
        while (start != NULL )
       {
    
          printf("\n%f %f\n", start->numberone, start->numbertwo);
    
          if (start->next != NULL)
          {
             total += sqrt( ( pow( (start->numberone - start->next->numberone), 2) + pow( (start->numbertwo - start->next->numbertwo), 2) ));
          }
          start = start->next;
    
        printf("%s %.2f", "\nTotal is now ", total);
    
       }
    when the following line:

    printf("\n%f %f\n", start->numberone, start->numbertwo);

    is placed inside the if statement it skips the last element. But it does not in the while loop.

  8. #23
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    That would definetly skip if you put that inside the if statement. I wouldnt explain why i know you know already(i hope).

    But if you put the total statement in your original code it would have been solved earlier. The problem woudnt be the last term not being printed out but the last term not being tallied correctly.
    Last edited by loko; 10-22-2005 at 08:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List question
    By Karpaty in forum C Programming
    Replies: 3
    Last Post: 01-08-2008, 01:54 PM
  2. Linked lists
    By coolio2006 in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2006, 04:36 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Replies: 1
    Last Post: 03-21-2002, 06:10 PM