0

I have the following code which is a nested linked list. I copied a sample program from google and tried to make it nested, but some how only the inner list is being printed and it gives segmentation fault. Below is my code

int main() {
         struct snapshot
        {
                int num;
                struct snapshot *next;
        };
        struct list_el {
                int val;
                struct  snapshot *snp;
                struct list_el * next;
        };

        typedef struct list_el item;
        typedef snapshot item2;
        item * curr, * head;
        item2 * curr2, * head2;
        int i , j;
        head = NULL;
        for(i=1;i<=10;i++) {
                curr = (item *)malloc(sizeof(item));
                curr->val = i;
                curr->next  = head;
                head = curr;
                for(j=1 ; j <= 5; j++)
                {
                        curr2 = (item2 *)malloc(sizeof(item2));
                        curr2->num = j;
                        curr2->next = head2;
                        head2 = curr2;
                }
                 curr->snp = head2;
        }
        curr = head;
        while(curr) {
            printf("begin\n");
            printf("%d\n", curr->val);
            curr2 = curr->snp;
            while(curr2)
            {
            head2 = NULL;
            printf("inner\n");
            printf("%d\n", curr2->num);
            //printf("%d\n",curr2->next);
            curr2 = curr2->next;
            }

            curr = curr->next ;
            printf("outer\n");
    }

        return 1;
}

I dont seem to understand the problem.

Muneeb Zulfiqar
  • 1,003
  • 2
  • 13
  • 31
  • 0) `typedef snapshot item2;` --> `typedef struct snapshot item2;` 1) `head = NULL;` --> `head = NULL;head2 = NULL;` – BLUEPIXY Apr 30 '15 at 12:56

2 Answers2

3

Shouldn't you head2 = NULL; ? Your second list will never end.

Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
2

A few things are not really good here.

First, you don't seem to initialize all your variables. In this case the missing initialization of head2 to NULL might be the culprit.

Also don't cast the result of malloc().

The second thing, the list isn't really nested at all, they're two seperate lists with no connection to each other.

Nested lists could be done with a more generic list element definition like

struct ListElement
{
  struct ListElement *pNext;
  void *pData;
};

with that you can set pData to be a pointer to another List head.

Also your "head" variables seem to more like pointers to the tail of the list rather than the head. So in your lists will be backwards.

EDIT: the fact that there is no connection between these lists also causes a memory leak, as you create a new second list for each list element of the first list, you also throw away the pointers of the previously generated second list, since you seem to not hold theses pointers anywhere else than "head2" and "curr2", which you overwrite every time a new list element for your first list is created.

rfreytag
  • 1,203
  • 11
  • 18
  • you need a new variable in your list element struct definition if you want to keep the number value in there seperately. so you need a generic pointer and then do curr->list = head2; after you're done with the loop for the second list. What do you mean with "cur->snp" ? – rfreytag Apr 30 '15 at 13:11
  • your change will likely result in a "undefined struct" error. do a declaration like "struct snapshot;" before the definition of the first list struct. and since head2 and curr2 are already pointers you don't want to use & just do curr->snp = head2; – rfreytag Apr 30 '15 at 13:13
  • how can i print the nested with list print on the elements of list2 on the current index? Still getting the same error.. Code edited again – Muneeb Zulfiqar Apr 30 '15 at 13:16
  • move the second print loop above curr = curr->next; and set curr2 = curr->snp; – rfreytag Apr 30 '15 at 13:18
  • you added head2 = NULL; in front of the creation loop though? EDIT: just tested, works fine. – rfreytag Apr 30 '15 at 13:25
  • Can you post the code please? I am a newbie and still learning how to deal with pointer. – Muneeb Zulfiqar Apr 30 '15 at 13:31
  • all I changed is I added the line "head2 = NULL;" in front of the for-loop which creates the second list. That fixed the segfault as described before. – rfreytag Apr 30 '15 at 13:33
  • @pfannkuchen_gesicht why not cast the result of malloc? I do that all the time. For one thing, it makes code more readable. Just curious. – dmedine Apr 30 '15 at 17:52
  • see http://stackoverflow.com/a/605858/2443844 basically it's neither necessary nor helpful. – rfreytag Apr 30 '15 at 19:30
  • Thanks! It is indeed bad if can hide a crash. I disagree that it makes the code harder to read, though. On the contrary, I think it makes it clearer -- especially in cases when you use a lot of `typedefs`.. FWIW... Also, I didn't know that `sizeof` isn't a function. that is good to know. – dmedine Apr 30 '15 at 19:53