2013-02-27 166 views
0

我试图使用节点添加到链接列表的前面如下:使用函数指针作为参数

struct Node *addFront(struct List *list, void *data) { 

到目前为止,我有以下几点:

struct Node *front = (struct Node *) malloc(sizeof(struct Node)){ 
    if(front == NULL) { 
     return NULL; 
    } 

    front->data = data; 

    if(list->head == 0) { 
     list->head = front; 
     front->next = NULL; 
    } 
    else { 
     list->head = front; 
     *front->next =* 
    } 

    return front; 
} 

我如果它不是第一个创建的节点,那么添加的节点会指向什么......我想说的是: front-> next = list; 但是List是List的类型,所以我确定我会得到一些不兼容的赋值错误。要做到这一点,最好的方法是什么?

回答

0
Node *oldHead = list->head; 
list->head = front; 
front->next =oldHead; 

存储旧的磁头,并将其分配给front->next

或者只是

front->next =list->head; 
list->head = front; 

就足够了。

+0

谢谢!我是一个新手(如你所见)。非常感谢。 – user1889966 2013-02-27 02:58:53