I am trying to implement a linked list in C. There is a struct list that has void pointers to the first and last position of the list. A struct node that has a void pointer to data and a struct node pointer to next. For some reason when I try and access the struct lists front pointer it seg faults. Here is the code. Any help will be appreciated.
(The main method initializes the list that is passed into the function as null)
int main()
{
struct list *linked_list;
linked_list = NULL;
int *ptr;
int x = 5;
ptr = &x;
linked_list = list_add(linked_list,ptr);
}
struct list {
void *front;
void *back;
};
struct node{
void *data;
struct node *next;
struct node *prev;
};
struct list *list_add(struct list *li, void *d){
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL){
printf("Malloc failed");
exit(1);
}
new_node->data = d;
new_node->next = NULL;
struct node *cur;
for(cur = li->front; cur != NULL; cur = cur->next){
if(cur->next == NULL){
new_node->prev = cur;
}
}
li->front = new_node;
return li;
}