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.