Linked lists; getting the current elements next element

This is a discussion on Linked lists; getting the current elements next element within the C Programming forums, part of the General Programming Boards category; how exactly is the next element of the current element accessed in a linked list? for some strange reason: Code: ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Linked lists; getting the current elements next element

    how exactly is the next element of the current element accessed in a linked list?
    for some strange reason:

    Code:
    printf("%s", start->next->custName);
    gives me a segmentation fault.

    The first element element is skipped, the second one is printed, and then thats when i get the segmentation fault.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    You should show how you are building your list and your list structure

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hi sand_man,

    i've already posted the code here

    but here's a slightly different version to show my problem:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct charNode
    {
       double numberone;
       double numbertwo;
       struct charNode *next;
    };
    
    typedef struct charNode charNode;
    
    void addElement(charNode **start, double numberone, double numbertwo);
    void printList(charNode *start);
    charNode* newNode(double numberone, double numbertwo);
    void getString(charNode *start);
    
    int main()
    {
       charNode *start = NULL;
    
       getString(start);
       printList(start);
       return 0;
    }
    
    
    void getString(charNode *start)
    {
    
         addElement(&start, 1, 1);
         addElement(&start, 2, 2);
         addElement(&start, 3, 3);
         printList(start);
    }
    
    void addElement(charNode **start, double numberone, double numbertwo)
    {
    
       charNode *temp,*prev, *loc;
    
       temp = newNode(numberone, numbertwo);
    
    
       if (*start == NULL)
       {
          *start = temp;
       }
    
       else
       {
          loc = *start;
          prev = NULL;
    
          while (loc != NULL)
          {
             prev = loc;
             loc = loc->next;
          }
    
    
          temp->next = loc;
          if (prev == NULL)
          {
             *start = temp;
          }
          else
          {
             prev->next = temp;
          }
       }
    
    }
    
    
    charNode* newNode(double numberone, double numbertwo)
    {
    
       charNode *temp;
    
       temp = (charNode *) malloc(sizeof(charNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
    
    
       temp->numberone = numberone;
       temp->numbertwo = numbertwo;
    
    
       temp->next = NULL;
    
       return temp;
    }
    
    
    void printList(charNode *start)
    {
    
       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;
    
       }
    
    
    /*
    
    1.000000 1.000000
    next element is -+>  2.00 2.00 **OK**
    2.000000 2.000000
    next element is -+>  3.00 3.00 **OK**
    3.000000 3.000000
    <--- BAM! segmentation fault.
    
    
    notice how the "next element..." is not printed in the first line?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    The answer is clear isn't it?

    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;
    }
    Imagine you've looped through and are at the last node in the list. The start pointer is valid, pointing to that last element. The parts of your code in red above however are attempting to access who-knows-what since the next pointer is NULL in those cases.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yes you're right but how exactly do i get a reference to the next element when its not null?

    Here's what actually happens:

    element one | start | next skips here for some reason
    element two -> start | next points here when it's first run
    element three-> next now points here second time
    element four -> ???

    it thinks that theres a last element because it skipped the first one and since there's no memory allocated to me in that area it throws a segmentation fault.

    How do i get it to start off at element one?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    It does not skip the next element the first time if your sample output is to be believed:

    1.000000 1.000000
    next element is -+> 2.00 2.00 **OK**

    2.000000 2.000000
    next element is -+> 3.00 3.00 **OK**

    3.000000 3.000000
    <--- BAM! segmentation fault.


    notice how the "next element..." is not printed in the first line?
    I don't get why you are saying "next element" is not printed in the first line. You do have a blank line printed before output really starts, but that is simply because of the beginning '\n' character you output in the printf("\n%f %f\n"... statement. Is that what you're talking about?

    Lines in blue would be displayed when start points to the 1st node of the linked list. Lines in green would be displayed when start points to the 2nd node of the linked list. Lines in red would be displayed when start points to the 3rd (and last) node of the linked list.

    Or am I missinterpretting what you are saying? Maybe I'm a bit unclear exactly what you are asking for?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok i see, it was a simple case of:
    Code:
       while (start != NULL )
       {
    
          printf("\n%f %f\n", start->numberone, start->numbertwo);
          if (start->next != NULL)
          {
             printf("%s %.2f %.2f %s", "next element is -+> ", start->next->numberone, start->next->numbertwo, "**OK**");
          }
          start = start->next;
    
       }

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i guess that worked, but it's not really what i wanted. With the following modification:

    Code:
     addElement(&start, 28.80, 17.60);
         addElement(&start, 5.60,  79.12);
         addElement(&start,  55.30, 49.90);
              addElement(&start,  22.22, 11.11);
    printed out:

    28.800000 17.600000

    5.600000 79.120000

    55.300000 49.900000

    as you can see the last element 22.22, 11.11 is skipped
    with the following code:

    Code:
      while (start != NULL )
       {
    
          if (start->next != 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;
    
       }
    is there anyway to print it out without skipping the last node? It stops because when it hits the last element and when start->next is run, its null so the if condition fails which causes the start->numberone print statement to fail, when it's actually not null.

  9. #9
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    What about

    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**");
         if (start->next == NULL)
              break;
                    
          start = start->next;
    
       }

  10. #10
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    sorry i wasn't quiet clear. Yes that works perfectly fine for the "current" elements. The problem is with the next elements, in the while condition when it gets to the last element in the list it will equal true, when i attempt to access the start->next->xxx it get a segmentation fault because there isn' t memory allocated to that area.

    i.e.

    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**");
         if (start->next == NULL)
              break;
    
          start = start->next;
    
       }

    28.800000 17.600000
    next element is -+> 5.60 79.12 **OK**
    5.600000 79.120000
    next element is -+> 55.30 49.90 **OK**
    55.300000 49.900000
    next element is -+> 22.22 11.11 **OK**
    22.220000 11.110000
    <it tries to access the next element after 22 which gives ->
    abnormal program termination


    what i was trying to do is access the current element, next element and not fall out of the list

    it's quiet trivial because;

    1. If you make a while condition saying that when start is not null, all the current elements will be printed but it will give an exception because at the last element the condition will equal true and then in the contents of the while loop it will attempt to access the next element

    2. If you make a while condition saying that when start->next is not null then the last element will be skipped all together because start->next will equal false after the 2nd last element.

    sorry, I hope i'm making sense
    Last edited by Axel; 10-22-2005 at 07:05 AM.

  11. #11
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Ofcourse you would get a segmentation Fault. Your trying to access memory space that doesnt exist.

    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**"); // If start->next is null this wont work. This is causing segmentation fault.
         if (start->next == NULL)
              break;
    
          start = start->next;
    
       }
    *Edited
    And also hk_mp5kpdw already answered the question above. Scroll up and read thouroughly.

    *Edited2
    Oops i guess you understand what he already said. I cant help much i really dont know what your trying to do.
    Last edited by loko; 10-22-2005 at 07:30 AM.

  12. #12
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Yep, based on what he said i figured it out. But for some reason:

    Code:
         while (start != NULL )
       {
    
          //printf("\n%f %f\n", start->numberone, start->numbertwo);
          if (start->next != NULL)
          {
             //printf("%s %.2f %.2f %s", "next element is -+> ", start->next->numberone, start->next->numbertwo, "**OK**");
             printf("\n%s%f%s%f ", "X is ", start->numberone, " Y is ", start->numbertwo );
             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);
    
       }

    X is 28.800000 Y is 17.600000
    Total is now 65.75
    X is 5.600000 Y is 79.120000
    Total is now 123.40
    X is 55.300000 Y is 49.900000
    Total is now 174.38
    Total is now 174.38

    why is the last element contents:
    22.22, 11.11
    skipped?

  13. #13
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    That is becoz


    Code:
    printf("\n%s%f%s%f ", "X is ", start->numberone, " Y is ", start->numbertwo );
    is inside

    Code:
    if (start->next != NULL)
    If you do it like this. I dont see the last being skipped here. But I bet you would still have a problem??

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

  14. #14
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Is this part importan to you?
    Code:
          total += sqrt( ( pow( (start->numberone - start->next->numberone), 2) + pow( (start->numbertwo - start->next->numbertwo), 2) ));
    what about put that out of the if( start-next==null). and replace the red part with zero if start-next would be null. There wouldnt be anything there anyway.


    x =0;

    if( start->next != null )
    x = pow( (start->numbertwo - start->next->numbertwo), 2) );

    Code:
          total += sqrt( ( pow( (start->numberone - start->next->numberone), 2) + x  )

  15. #15
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yes, the total should be 183. You're right the last element is printed now but it is not being added to total for some reason.

Page 1 of 2 12 LastLast
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, 12: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, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21