Finding LinkedList Size

This is a discussion on Finding LinkedList Size within the C Programming forums, part of the General Programming Boards category; I'm simply trying to find the length of the linked list, but my findSize() function isn't working right and I'm ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Finding LinkedList Size

    I'm simply trying to find the length of the linked list, but my findSize() function isn't working right and I'm not sure where it's going wrong...I get a segmentation fault when i run it, so probably out of bounds somewhere...just where?

    Code:
    typedef struct node {
      char* value;
      struct node* next;
    } LinkedList;
    
    int main() {
    
      char str1[] = {'t', 'e', 's', 't', '\0'};
    
      LinkedList* ll = llCreate();
      
      llAdd(&ll, str1);
      
      
      int* size;
      findSize(ll, size);  
      printf("%d", *size);
      
    }
    
    void findSize(LinkedList* ll, int* size) {
      LinkedList* p = ll;
    
      while(p != NULL){
        (*size)++;
        p = p->next;
      }
    }
    
    LinkedList* llCreate() {
      return NULL;
    }
    
    void llAdd(LinkedList** ll, char* value) {
    
      LinkedList* nn = (LinkedList*)malloc(sizeof(LinkedList));
      nn->value = value;
    
      if (*ll == NULL) {
        *ll = nn;
      } else {
        // General case - find the end
        LinkedList* p = *ll;
        while (p->next != NULL) {
          p = p->next;
        }
        p->next = nn;
      }
    }
    Last edited by johngoodman; 02-17-2013 at 11:06 AM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    If size in main() is a pointer, what is it pointing to?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I would say he is confused with the fact that you have to pass a variable by reference, so that the modification remains after the function terminates.

    You should declare the variable as just an int and call the function like this
    Code:
    findSize(ll, &size);
    Your function is nice, it works

    But, there is a logical error there, isn't?
    You increment your variable, but which is its initial variable? Initialize it to zero and you would be fine
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by std10093 View Post
    I would say he is confused with the fact that you have to pass a variable by reference, so that the modification remains after the function terminates.

    You should declare the variable as just an int and call the function like this
    Code:
    findSize(ll, &size);
    Your function is nice, it works

    But, there is a logical error there, isn't?
    You increment your variable, but which is its initial variable? Initialize it to zero and you would be fine


    That fixed the segmentation fault, thank you, but now my size is only incrementing to 1 and then not looping through the while loop

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I would guess the problem is at your list. Did you print the list and prints the whole list?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by std10093 View Post
    I would guess the problem is at your list. Did you print the list and prints the whole list?
    huh, where?? The while loop in my findSize() function is only going through once

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    From your code above, 1 would be the correct value. You only add one node to your list.

    You should always check your pointer to malloc'd memory for NULL before attempting to use it.

    LinkedList is a misleading name for a node struct. Is a struct a linked list?

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by Tclausex View Post
    From your code above, 1 would be the correct value. You only add one node to your list.

    You should always check your pointer to malloc'd memory for NULL before attempting to use it.

    LinkedList is a misleading name for a node struct. Is a struct a linked list?
    Oops, forgot it is a string not a bunch of characters it is reading in, i just tested with multiple strings and it works, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the file size.
    By +Azazel+ in forum C Programming
    Replies: 18
    Last Post: 10-16-2007, 09:21 AM
  2. Finding the size of the borders.
    By jpfarias in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 08:50 PM
  3. Finding Words in a array[size][size]
    By ^DJ_Link^ in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 02:51 PM
  4. finding size of char*
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2005, 12:30 AM
  5. Finding the size of a directory
    By J_Bravo in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 07:36 AM

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