在C中为链表中的头节点赋值NULL

在C中为链表中的头节点赋值NULL,c,function,pointers,linked-list,reference,C,Function,Pointers,Linked List,Reference,请参阅下面的完整代码 我有一个名为arr的初始数组。 我使用链表通过append函数存储一些索引。获取索引后,我将它们存储在链表中,并使用clearList将相应的值更改为0(在本例中为arr[2]和arr[4])。 最后,我通过调用freeList释放内存,因为我已经完成了链表 然而,为了能够一次又一次地做同样的事情,每当我调用自由列表时,我需要将head设置为NULL。但我不能。你知道怎么解决这个问题吗? 多谢各位 #include <stdio.h> #include "g

请参阅下面的完整代码

我有一个名为
arr
的初始数组。 我使用链表通过
append
函数存储一些索引。获取索引后,我将它们存储在链表中,并使用
clearList
将相应的值更改为0(在本例中为arr[2]和arr[4])。 最后,我通过调用
freeList
释放内存,因为我已经完成了链表

然而,为了能够一次又一次地做同样的事情,每当我调用
自由列表时,我需要将
head
设置为NULL。但我不能。你知道怎么解决这个问题吗? 多谢各位

#include <stdio.h>  
#include "gurobi_c.h"
#include <stdlib.h>

//Gurobi variables
GRBenv   *env = NULL;
GRBmodel *model = NULL;
//Gurobi variables

struct Node 
{ 
  int data; 
  struct Node *next;
  struct Node *end;
};

void append(struct Node** head_ref, int new_data) 
    { 
    struct Node *last = *head_ref;  
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));  
    new_node->data  = new_data; 
    new_node->next = NULL;
    new_node->end = new_node;
    if (*head_ref == NULL) 
    { 
       *head_ref = new_node;
       //printf("  ..Init Append %d\n",new_data);
       return; 
    } 
    last = (*head_ref)->end; 
    last->next = new_node;  
    (*head_ref)->end=new_node;
    //printf("  ..Append %d\n",new_data);
    return; 
} 

void clearList(struct Node *node, double *arr) 
    { 
    int i;
    if(node!=NULL)
        {
        struct Node tmp;
        tmp=*(node->end);
        while (node != NULL) 
            {  
            i=node->data;
            arr[i]=0;
            //printf("   ..clear %d \n", node->data,(node->end)->data);
            node = node->next; 
            }
        }
    }

void freeList(struct Node *node) 
    { 
    struct Node *tmp,*hd;
    hd=node;
    while (node != NULL) 
        { 
        tmp=node;
        node = node->next; 
        //printf("  ..Free %d \n", tmp->data);      
        free(tmp);
        } 
    hd=NULL;
    }

int main (){
    Node *head;     
    double *arr = (double *) malloc(sizeof(double) * 10);
    for(int i=0;i<10;i++)
        arr[i]=i;

    head=NULL;
    printf("Head:  %s\n", head);
    append(&head,2);
    append(&head,4);
    clearList(head,arr);
    for(int i=0;i<10;i++)
        printf("No %d : %.2f\n",i,arr[i]);
    freeList(head);

    free(arr);

    printf("%s", head);
    getchar();
    return 0;
    }
#包括
#包括“gurobi_c.h”
#包括
//古罗比变量
GRBenv*env=NULL;
GRBmodel*model=NULL;
//古罗比变量
结构节点
{ 
int数据;
结构节点*下一步;
结构节点*结束;
};
void append(结构节点**head\u ref,int new\u数据)
{ 
结构节点*last=*head\u ref;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=新建_数据;
新建节点->下一步=空;
新建\u节点->结束=新建\u节点;
如果(*head_ref==NULL)
{ 
*head\u ref=新节点;
//printf(“…Init Append%d\n”,新的\u数据);
回来
} 
最后=(*头部参考)->末端;
last->next=新建_节点;
(*head\u ref)->end=新节点;
//printf(“…附加%d\n”,新的\u数据);
回来
} 
void clearList(结构节点*Node,双*arr)
{ 
int i;
如果(节点!=NULL)
{
结构节点tmp;
tmp=*(节点->结束);
while(节点!=NULL)
{  
i=节点->数据;
arr[i]=0;
//printf(“…清除%d\n”,节点->数据,(节点->结束->数据);
节点=节点->下一步;
}
}
}
无效自由列表(结构节点*节点)
{ 
结构节点*tmp,*hd;
hd=节点;
while(节点!=NULL)
{ 
tmp=节点;
节点=节点->下一步;
//printf(“…自由%d\n”,tmp->数据);
免费(tmp);
} 
hd=NULL;
}
int main(){
节点*头;
double*arr=(double*)malloc(sizeof(double)*10);

对于(int i=0;i我意识到可以更改
freeList
函数,使其返回空值。请参阅下面更新的代码:

#include <stdio.h>  
#include "gurobi_c.h"
#include <stdlib.h>

//Gurobi variables
GRBenv   *env = NULL;
GRBmodel *model = NULL;
//Gurobi variables

struct Node 
{ 
  int data; 
  struct Node *next;
  struct Node *end;
};

void append(struct Node** head_ref, int new_data) 
    { 
    struct Node *last = *head_ref;  
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));  
    new_node->data  = new_data; 
    new_node->next = NULL;
    new_node->end = new_node;
    if (*head_ref == NULL) 
    { 
       *head_ref = new_node;
       //printf("  ..Init Append %d\n",new_data);
       return; 
    } 
    last = (*head_ref)->end; 
    last->next = new_node;  
    (*head_ref)->end=new_node;
    //printf("  ..Append %d\n",new_data);
    return; 
} 

void clearList(struct Node *node, double *arr) 
    { 
    int i;
    if(node!=NULL)
        {
        struct Node tmp;
        tmp=*(node->end);
        while (node != NULL) 
            {  
            i=node->data;
            arr[i]=0;
            //printf("   ..clear %d \n", node->data,(node->end)->data);
            node = node->next; 
            }
        }
    }

Node* freeList(struct Node *node) 
    { 
    struct Node *tmp;
    while (node != NULL) 
        { 
        tmp=node;
        node = node->next; 
        printf("  ..Free %d \n", tmp->data);      
        free(tmp);
        } 
    return NULL;
    }

int main (){
    Node *head;     
    double *arr = (double *) malloc(sizeof(double) * 10);
    for(int i=0;i<10;i++)
        arr[i]=i;

    head=NULL;
    printf("Head:  %s -> null as expected\n", head);
    append(&head,2);
    append(&head,4);
    clearList(head,arr);
    for(int i=0;i<10;i++)
        printf("No %d : %.2f\n",i,arr[i]);

    printf("Head:  %s -> Not null as linkedlist is not freed\n", head);
    head=freeList(head);
    printf("Head:  %s -> Again null as expected\n", head);
    free(arr);
    printf("%s", head);
    getchar();
    return 0;
    }
#包括
#包括“gurobi_c.h”
#包括
//古罗比变量
GRBenv*env=NULL;
GRBmodel*model=NULL;
//古罗比变量
结构节点
{ 
int数据;
结构节点*下一步;
结构节点*结束;
};
void append(结构节点**head\u ref,int new\u数据)
{ 
结构节点*last=*head\u ref;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=新建_数据;
新建节点->下一步=空;
新建\u节点->结束=新建\u节点;
如果(*head_ref==NULL)
{ 
*head\u ref=新节点;
//printf(“…Init Append%d\n”,新的\u数据);
回来
} 
最后=(*头部参考)->末端;
last->next=新建_节点;
(*head\u ref)->end=新节点;
//printf(“…附加%d\n”,新的\u数据);
回来
} 
void clearList(结构节点*Node,双*arr)
{ 
int i;
如果(节点!=NULL)
{
结构节点tmp;
tmp=*(节点->结束);
while(节点!=NULL)
{  
i=节点->数据;
arr[i]=0;
//printf(“…清除%d\n”,节点->数据,(节点->结束->数据);
节点=节点->下一步;
}
}
}
节点*自由列表(结构节点*节点)
{ 
结构节点*tmp;
while(节点!=NULL)
{ 
tmp=节点;
节点=节点->下一步;
printf(“…自由%d\n”,tmp->数据);
免费(tmp);
} 
返回NULL;
}
int main(){
节点*头;
double*arr=(double*)malloc(sizeof(double)*10);

对于(int i=0;i我意识到可以更改
freeList
函数,使其返回空值。请参阅下面更新的代码:

#include <stdio.h>  
#include "gurobi_c.h"
#include <stdlib.h>

//Gurobi variables
GRBenv   *env = NULL;
GRBmodel *model = NULL;
//Gurobi variables

struct Node 
{ 
  int data; 
  struct Node *next;
  struct Node *end;
};

void append(struct Node** head_ref, int new_data) 
    { 
    struct Node *last = *head_ref;  
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));  
    new_node->data  = new_data; 
    new_node->next = NULL;
    new_node->end = new_node;
    if (*head_ref == NULL) 
    { 
       *head_ref = new_node;
       //printf("  ..Init Append %d\n",new_data);
       return; 
    } 
    last = (*head_ref)->end; 
    last->next = new_node;  
    (*head_ref)->end=new_node;
    //printf("  ..Append %d\n",new_data);
    return; 
} 

void clearList(struct Node *node, double *arr) 
    { 
    int i;
    if(node!=NULL)
        {
        struct Node tmp;
        tmp=*(node->end);
        while (node != NULL) 
            {  
            i=node->data;
            arr[i]=0;
            //printf("   ..clear %d \n", node->data,(node->end)->data);
            node = node->next; 
            }
        }
    }

Node* freeList(struct Node *node) 
    { 
    struct Node *tmp;
    while (node != NULL) 
        { 
        tmp=node;
        node = node->next; 
        printf("  ..Free %d \n", tmp->data);      
        free(tmp);
        } 
    return NULL;
    }

int main (){
    Node *head;     
    double *arr = (double *) malloc(sizeof(double) * 10);
    for(int i=0;i<10;i++)
        arr[i]=i;

    head=NULL;
    printf("Head:  %s -> null as expected\n", head);
    append(&head,2);
    append(&head,4);
    clearList(head,arr);
    for(int i=0;i<10;i++)
        printf("No %d : %.2f\n",i,arr[i]);

    printf("Head:  %s -> Not null as linkedlist is not freed\n", head);
    head=freeList(head);
    printf("Head:  %s -> Again null as expected\n", head);
    free(arr);
    printf("%s", head);
    getchar();
    return 0;
    }
#包括
#包括“gurobi_c.h”
#包括
//古罗比变量
GRBenv*env=NULL;
GRBmodel*model=NULL;
//古罗比变量
结构节点
{ 
int数据;
结构节点*下一步;
结构节点*结束;
};
void append(结构节点**head\u ref,int new\u数据)
{ 
结构节点*last=*head\u ref;
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建_节点->数据=新建_数据;
新建节点->下一步=空;
新建\u节点->结束=新建\u节点;
如果(*head_ref==NULL)
{ 
*head\u ref=新节点;
//printf(“…Init Append%d\n”,新的\u数据);
回来
} 
最后=(*头部参考)->末端;
last->next=新建_节点;
(*head\u ref)->end=新节点;
//printf(“…附加%d\n”,新的\u数据);
回来
} 
void clearList(结构节点*Node,双*arr)
{ 
int i;
如果(节点!=NULL)
{
结构节点tmp;
tmp=*(节点->结束);
while(节点!=NULL)
{  
i=节点->数据;
arr[i]=0;
//printf(“…清除%d\n”,节点->数据,(节点->结束->数据);
节点=节点->下一步;
}
}
}
节点*自由列表(结构节点*节点)
{ 
结构节点*tmp;
while(节点!=NULL)
{ 
tmp=节点;
节点=节点->下一步;
printf(“…自由%d\n”,tmp->数据);
免费(tmp);
} 
返回NULL;
}
int main(){
节点*头;
double*arr=(double*)malloc(sizeof(double)*10);

对于(int i=0;i您已经在
append
函数中更改了head的值,因此您基本上需要在
自由列表中执行相同的操作:

void freeList(struct Node **head_ref) 
    { 
    struct Node *tmp,*node;
    node=*head_ref;
    while (node != NULL) 
        { 
        tmp=node;
        node = node->next; 
        //printf("  ..Free %d \n", tmp->data);      
        free(tmp);
        } 
    *head_ref=NULL;
    }

int main (){
    /* do stuff */
    freeList(&head);
    /* do stuff */
    }

您已经在
append
函数中更改了head的值,因此您基本上需要在
自由列表中执行相同的操作:

void freeList(struct Node **head_ref) 
    { 
    struct Node *tmp,*node;
    node=*head_ref;
    while (node != NULL) 
        { 
        tmp=node;
        node = node->next; 
        //printf("  ..Free %d \n", tmp->data);      
        free(tmp);
        } 
    *head_ref=NULL;
    }

int main (){
    /* do stuff */
    freeList(&head);
    /* do stuff */
    }

为了完整性:另一个可能的选择是为
freeList()
使用包装宏

此解决方案与返回修改过的指针的版本具有类似的缺点。您可以简单地忘记使用righ