Hi,
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,
struct linked_list {
int data;
struct linked_list next;
}
I thought of two arrays of pointers
struct linked_list **first;
struct linked_list **last;
in order to acces the linked_lists.
I have the following methods,
void
init_lists(cons t int N)
{
first = (struct linked_particle **)calloc(N, sizeof(struct
linked_particle *));
last = (struct linked_particle **)malloc(N * sizeof(struct
linked_particle *));
int i;
for (i = 0; i < N; i++) {
struct linked_particle *nova =
(struct linked_list *)malloc(sizeof (struct linked_list));
first[i] = last[i] = nova;
}
}
and
struct linked_list *
cells_get_cell_ pointer (const int i)
{
struct linked_list *to_return = NULL;
to_return = first[i];
return to_return;
}
there is also a method for putting more elements in a given list,
void
add_element_to_ list (const int i,
const int data)
{
struct linked_list *nova =
(struct linked_list *)malloc(sizeof (struct linked_particle ));
nova->data = data;
struct linked_particle *last_actual = last[i];
if (!last_actual) printf("Error\n ");
linked_particle _add(nova,&last _actual);
return;
}
where
void
linked_particle _add (struct linked_list * to_add,
struct linked_list ** last){
if(!*last) *last = to_add;
else (*last)->next = to_add;
to_add->next = NULL;
*last = to_add;
return;
}
The program seems to work when I add new items to a given list (I get
no errors), but when I try to get the first pointer of a given list in
order to list all the elements, I only get a NULL pointer.
I think I'm missing something, but I don't know what.
Thanks,
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,
struct linked_list {
int data;
struct linked_list next;
}
I thought of two arrays of pointers
struct linked_list **first;
struct linked_list **last;
in order to acces the linked_lists.
I have the following methods,
void
init_lists(cons t int N)
{
first = (struct linked_particle **)calloc(N, sizeof(struct
linked_particle *));
last = (struct linked_particle **)malloc(N * sizeof(struct
linked_particle *));
int i;
for (i = 0; i < N; i++) {
struct linked_particle *nova =
(struct linked_list *)malloc(sizeof (struct linked_list));
first[i] = last[i] = nova;
}
}
and
struct linked_list *
cells_get_cell_ pointer (const int i)
{
struct linked_list *to_return = NULL;
to_return = first[i];
return to_return;
}
there is also a method for putting more elements in a given list,
void
add_element_to_ list (const int i,
const int data)
{
struct linked_list *nova =
(struct linked_list *)malloc(sizeof (struct linked_particle ));
nova->data = data;
struct linked_particle *last_actual = last[i];
if (!last_actual) printf("Error\n ");
linked_particle _add(nova,&last _actual);
return;
}
where
void
linked_particle _add (struct linked_list * to_add,
struct linked_list ** last){
if(!*last) *last = to_add;
else (*last)->next = to_add;
to_add->next = NULL;
*last = to_add;
return;
}
The program seems to work when I add new items to a given list (I get
no errors), but when I try to get the first pointer of a given list in
order to list all the elements, I only get a NULL pointer.
I think I'm missing something, but I don't know what.
Thanks,
Comment