Hello, I am making an application that implements doubly linked lists (each node has a pointer to the previous node and the next node), and I am am having trouble with the functions that service the list. I am getting a compiler error "dereferencing pointer to incomplete type", and I am unsure of how to proceed.
Here is the relevant code
In list.h
Code:
/* linked list node */
typedef struct _lnode {
struct lnode *prev;
struct lnode *next;
int data;
} lnode;
/* struct for list, contains a pointer to the head of the list */
typedef struct _llist {
lnode *head;
} llist;
in list.c
Code:
/* adds a node to the front of the list given a pointer to the list and the node to add
* replaces current head.
*/
void add_front(llist* list, lnode* node) {
/* new head needs to point to old head as its next.
* new head needs to point to old tail as its prev.
* old head needs to point to new head as its prev.
* tail needs to point to new head as its next.
* list needs to recognize new head as head.
*/
node->next = (struct lnode *) list->head;
node->prev = list->head->prev;
//ERROR OCCURS NEXT LINE
list->head->prev->next = node;
list->head->prev = (struct lnode *)node;
list->head = node;
}
list.h is included, so the compiler can find everything okay, so I don't know what 'incomplete type'
is referring to. On a side note, if I change
Code:
typedef struct _llist {
lnode *head;
} llist;
to
Code:
typedef struct _llist {
struct lnode *head;
} llist;
I will get a lot of the same error on different statements in the add_front function. I am not sure why this happens either. Is there something fundamental about pointers/structs that I am not getting? Thanks in advance.