I get this:
LLIST *mylist[N];
Where N is the number of rows of the input file. Then mylist[i]
is a pointer to the ith linked list.
I call a function
LLIST *list_add(LLIST **p, int i){
LLIST *n;
n = (LLIST *) malloc(sizeof(LLIST));
if (n == NULL)
return NULL;
n->next = *p; /* the previous element (*p) now becomes the "next" element */
*p = n; /* add new empty element to the front (head) of the list */
n->data = i;
return p;
}
So in my Main I say something like
LLIST *mylist[N];
list_add(&mylist[0],1);
list_add(&mylist[0],2);
list_add(&mylist[1],3)
list_add(&mylist[1],4);
list_print(mylist[0]); // Print mylist[0]
list_print(mylist[1]); // Print mylist[1]
My Print_list function is:
Void *list_print(LLIST *n) {
if (n == NULL){
printf("list is empty\n");
}
while (n != NULL){
printf("%d",n->data);
n = n->next;
}
}
When I do list_print(mylist[0])
, it prints out 2 1
.
When I do list_print(mylist[1])
, I get a segmentation fault.
What is going on?