我正在尝试创建一个可遍历的链表,但是当我尝试向列表中添加一个元素时我的程序崩溃了。我已经尝试了很多事情,但我无法弄清楚我做错了什么。
typedef struct node_t node_t;
typedef struct list_t list_t;
struct node_t {
void *data;
node_t *prev;
node_t *next;
};
struct list_t {
node_t front;
node_t back;
};
list_t* listEmpty () {
list_t *list = malloc (sizeof (list_t));
list->front.prev = NULL;
list->front.next = &(list->back);
list->back.prev = &(list->front);
list->back.next = NULL;
return list;
}
void listPushFront (list_t *list, void *data) {
node_t *newNode = malloc (sizeof (node_t));
newNode->data = data;
newNode->prev = &(list->front);
newNode->next = list->front.next;
list->front.next->prev = newNode; //CRASHES HERE
list->front.next = newNode;
}
int main (int argc, char *argv[]) {
list_t *list = malloc (sizeof (list_t));
int a = 6;
void* pa = &a;
listEmpty(list);
listPushFront (list, pa);
return 0;
}