Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么此函数不能成功地将节点插入到链接列表中?_C_List_Pointers_Linked List_Stack - Fatal编程技术网

C 为什么此函数不能成功地将节点插入到链接列表中?

C 为什么此函数不能成功地将节点插入到链接列表中?,c,list,pointers,linked-list,stack,C,List,Pointers,Linked List,Stack,该函数应该将节点插入到链接列表中,但它有一个bug,并且插入的节点不会显示在链接列表中。臭虫在哪里 int insert(struct Node *headList, int payload) { struct Node *newNode; newNode = malloc(sizeof(struct Node)); assert (newNode != NULL); newNode->payload = payload; newNode->next =

该函数应该将节点插入到链接列表中,但它有一个bug,并且插入的节点不会显示在链接列表中。臭虫在哪里

int insert(struct Node *headList, int payload) {
   struct Node *newNode;
   newNode = malloc(sizeof(struct Node));
   assert (newNode != NULL);
   newNode->payload = payload;
   newNode->next = headList;
   headList = newNode;
   return 0;
}

我很确定
Node*headList
是通过值传递的,因为
headList
在这个函数之外不会改变,但我不确定如何解决这个问题。我需要指向指针的指针吗?

我看不到任何
return
语句的用法,所以与其使用指向指针的指针,不如将更新的
标题列表
指针返回给调用者

 struct Node * insert(struct Node *headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert(newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
    headList = newNode;
    return headList;
}

 //in caller function
  //Some code
  headList = insert(headList,100);
如果您对
return
语句非常具体,请使用指针到指针

int insert(struct Node **headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert (newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
   *headList = newNode;
    return 0;
}

//in caller function
//Some code
status = insert(&headList,100);

我看不到任何使用
return
语句的地方,所以不要使用指向指针的指针,而是返回更新后的
headList
指向调用者的指针

 struct Node * insert(struct Node *headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert(newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
    headList = newNode;
    return headList;
}

 //in caller function
  //Some code
  headList = insert(headList,100);
如果您对
return
语句非常具体,请使用指针到指针

int insert(struct Node **headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert (newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
   *headList = newNode;
    return 0;
}

//in caller function
//Some code
status = insert(&headList,100);

我看不到任何使用
return
语句的地方,所以不要使用指向指针的指针,而是返回更新后的
headList
指向调用者的指针

 struct Node * insert(struct Node *headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert(newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
    headList = newNode;
    return headList;
}

 //in caller function
  //Some code
  headList = insert(headList,100);
如果您对
return
语句非常具体,请使用指针到指针

int insert(struct Node **headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert (newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
   *headList = newNode;
    return 0;
}

//in caller function
//Some code
status = insert(&headList,100);

我看不到任何使用
return
语句的地方,所以不要使用指向指针的指针,而是返回更新后的
headList
指向调用者的指针

 struct Node * insert(struct Node *headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert(newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
    headList = newNode;
    return headList;
}

 //in caller function
  //Some code
  headList = insert(headList,100);
如果您对
return
语句非常具体,请使用指针到指针

int insert(struct Node **headList, int payload) {
    struct Node *newNode;
    newNode = malloc(sizeof(struct Node));
    assert (newNode != NULL);
    newNode->payload = payload;
    newNode->next = headList;
   *headList = newNode;
    return 0;
}

//in caller function
//Some code
status = insert(&headList,100);

此函数用于修改此指针指向的结构节点:

int insert(struct Node *headList, int payload) {
    ...
    headList = newNode;
    ...
}
但是,要修改指针本身,您需要传递指针地址以初始化指针到指针:

int insert(struct Node **headList, int payload) {
    ...
    *headList = newNode;  // <--  modifies the pointer itself
    ...
}
int-insert(结构节点**标题列表,int-payload){
...

*headList=newNode;//此函数修改此指针指向的
struct节点

int insert(struct Node *headList, int payload) {
    ...
    headList = newNode;
    ...
}
但是,要修改指针本身,您需要传递指针地址以初始化指针到指针:

int insert(struct Node **headList, int payload) {
    ...
    *headList = newNode;  // <--  modifies the pointer itself
    ...
}
int-insert(结构节点**标题列表,int-payload){
...

*headList=newNode;//此函数修改此指针指向的
struct节点

int insert(struct Node *headList, int payload) {
    ...
    headList = newNode;
    ...
}
但是,要修改指针本身,您需要传递指针地址以初始化指针到指针:

int insert(struct Node **headList, int payload) {
    ...
    *headList = newNode;  // <--  modifies the pointer itself
    ...
}
int-insert(结构节点**标题列表,int-payload){
...

*headList=newNode;//此函数修改此指针指向的
struct节点

int insert(struct Node *headList, int payload) {
    ...
    headList = newNode;
    ...
}
但是,要修改指针本身,您需要传递指针地址以初始化指针到指针:

int insert(struct Node **headList, int payload) {
    ...
    *headList = newNode;  // <--  modifies the pointer itself
    ...
}
int-insert(结构节点**标题列表,int-payload){
...


*headList=newNode;//返回更新的
headList
指向调用者的指针….或按地址传递头节点(指向指针的指针)并通过取消引用进行更新。返回更新的
headList
指向调用者的指针….或按地址传递头节点(指向指针的指针)并通过取消引用进行更新。返回更新的
headList
指向调用者的指针…或按地址传递头节点(指向指针的指针)并通过取消引用进行更新。返回更新的
headList
指向调用者的指针…或按地址传递头节点(指向指针的指针)+1我几乎总是使用这种方法,而不是返回值思想,因为我的返回值从来都不是无用的。它和大多数C库一样,在成功时返回零,在错误时返回非零。将其传回调用端值得付出额外的努力,通过地址imho传递头指针。@WhozCraig:问题是他的函数总是返回0已同意。我的观点是它不应该返回错误/成功状态,并在执行此操作时使用head ptr by address方法。潜在的错误并不总是简单的分配失败。可能是逻辑错误中止插入,但使用head指针是返回值对调用方完全隐藏这一事实。您的示例是正确的,这是应该做的,如果可以的话,我会再次投票。感谢您的回答并感谢您的评论!+1我几乎总是使用这种方法,而不是返回值思想,因为我的返回值从来都不是无用的。它与许多C库一样,returns在成功时为零,在错误时为非零。将其传回调用方值得付出额外的努力,通过地址imho传递头指针。@WhozCraig:问题是他的函数总是返回0已同意。我的观点是它不应该返回错误/成功状态,并在这样做时使用你的头ptr by address方法。A potential错误并不总是一个微不足道的分配失败。它可能是一个逻辑错误,中止了插入,但使用头指针返回值对调用方完全隐藏了这一事实。您的示例是正确的,并且是应该这样做的,如果可以的话,我将再次投票。感谢您的回答,并感谢您的评论+1我几乎总是使用这种方法,而不是返回值思想,因为我的返回值从来都不是无用的。它和许多C库一样,在成功时返回零,在错误时返回非零。将其传回调用端值得付出额外的努力,通过地址imho传递头指针。@WhozCraig:问题在于他的功能n始终返回0已同意。我的观点是它不应该返回错误/成功状态,并在执行此操作时使用head ptr by address方法。潜在错误并不总是一个微不足道的分配失败。可能是一个逻辑错误中止了插入,但使用head pointer is返回值完全隐藏了这一事实我是来电者。你的示例是正确的,也是应该这样做的,如果可以的话,我会再次投票。谢谢你的回答,感谢你的评论!+1我几乎总是使用这种方法,而不是返回值思想,因为我的返回值从来都不是无用的。它和大多数C库一样,返回ze