【问题标题】:deleting a node passed by reference in c删除c中通过引用传递的节点
【发布时间】:2016-11-04 00:36:46
【问题描述】:

请我尝试删除一个按值传递并由函数返回的节点,但头部是通过引用传递的。我尝试了这段代码,但编译器抱怨: *head = *head->next;

代码如下:

#include<stdio.h>


typedef struct nody student;


struct nody{

    char name[20];

    double gpa;

    student *next;

};

student* deletefirstlist(student **head, student *nn);

int main(){

    student *head, *node, *w;

    node = (student*) malloc(sizeof(student));
    gets(node->name);
    node->gpa=3.6;
    node->next=NULL;

    head = node;

    node = (student*) malloc(sizeof(student));
    gets(node->name);
    node->gpa=3.7;
    node->next=head;

    head = node;

    w = head;
    while(w!=NULL){
        printf("%s %lf\n\n", w->name, w->gpa);
        w=w->next;
    }
    node = deletefirstnode(&head,  node);

    while(w!=NULL){
        printf("%s %lf\n\n", w->name, w->gpa);
        w=w->next;
    }
    return 0;
}

student* deletefirstlist(student **head, student *nn){


    nn = *head;
    *head = *head->next; // the problem is here
    nn->next=NULL;

    return nn;
}

感谢一百万

【问题讨论】:

  • *head-&gt;next --> (*head)-&gt;next
  • 我试着做 *head = nn-> next;
  • ,它奏效了。对吗?
  • 在这种情况下(*head)-&gt;nextnn-&gt; next 是相同的。

标签: c linked-list


【解决方案1】:

从引用的代码中,我了解到您创建了一个带有头和链接节点的列表,然后您尝试删除第一个(头)节点。如果是这样,那么 您的代码必须更正为以下内容:

   #include<stdio.h>


    typedef struct nody student;


     struct nody{

       char name[20];

       double gpa;

       student *next;

     };

   student *deletefirstnode(student *head, student *nn);

   int main(){

    student *head, *node, *w;

    node = (student*) malloc(sizeof(student));
    gets(node->name);
    node->gpa=3.6;
    node->next=NULL;

    head = node;

    node = (student*) malloc(sizeof(student));
    gets(node->name);
    node->gpa=3.7;
    head->next=node;
    node->next=NULL;

    //  head->next = node (The first node is the head node)

    w = head;
    while(w!=NULL){
     printf("%s %lf\n\n", w->name, w->gpa);
     w=w->next;
    }
    node = deletefirstnode(head,  node);
    free(head); //to free up the memory of the old first node
    w=node;  //reset w to point to the new First node of the list
    while(w!=NULL){
     printf("%s %lf\n\n", w->name, w->gpa);
     w=w->next;
     }
    return 0; 
   }

   student *deletefirstnode(student *head, student *nn){
      nn = head->next;
      return nn;
   }

希望这些帮助。

【讨论】:

  • 非常感谢它确实有帮助!几乎是免费的(头);陈述。我们是否必须这样做,因为据我所知,如果我们失去头部,链表就会丢失。如果我错了,请纠正我。
【解决方案2】:

整体校正样本

#include <stdio.h>
#include <stdlib.h> //need this

typedef struct nody student;

struct nody{
    char name[20];
    double gpa;
    student *next;
};

student* deletefirstlist(student **head, student *nn);

int main(){
    student *head, *node, *w;

    node = (student*) malloc(sizeof(student));//casting is not necessary in C.
    *node->name = 0;
    //gets has already been abolished.
    while(1 != scanf("%19[^\n]%*c", node->name)){
        printf("input name\n");
        scanf("%*[^\n]");scanf("%*c");
    }
    node->gpa=3.6;
    node->next=NULL;

    head = node;

    node = (student*) malloc(sizeof(student));
    *node->name = 0;
    while(1 != scanf("%19[^\n]%*c", node->name)){
        printf("input name\n");
        scanf("%*[^\n]");scanf("%*c");
    }
    node->gpa=3.7;
    node->next=head;

    head = node;

    w = head;
    while(w != NULL){
        printf("%s %lf\n\n", w->name, w->gpa);
        w = w->next;
    }
    node = deletefirstlist(&head,  NULL);//The argument node is not necessary substantially. also function name is typo.

    free(node);

    w = head;//you forgot this
    while(w != NULL){
        printf("%s %lf\n\n", w->name, w->gpa);
        w = w->next;
    }
    //free rest
    return 0;
}

student* deletefirstlist(student **head, student *nn){
    nn = *head;
    if(*head){//NOT NULL
        *head = (*head)->next; // *head->next meant *(head->next)
        nn->next=NULL;
    }

    return nn;
}

【讨论】:

  • "%19[^\n]%*c" 如果在不输入任何其他字符的情况下按回车,则无法正常工作
  • 是的,当然。您可以为此授予支票。或 输入名称,您应该在其中输入名称。 :D
【解决方案3】:

考虑到编译器抱怨,这将解决它

*head = (*head)->next;

但是代码还有其他问题,例如您没有为函数返回的已删除节点释放内存:

node = deletefirstnode(&head,  node);
free(node); //missing
w=head; //w still points to the deleted node, point it to the new head 
while(w!=NULL){

BLUEPIXY 的代码解决了所有这些问题。

如果你使用 C++,我可以建议使用字符串作为名称,那么你可以轻松地将输入作为

getline(cin,name);

你也可以将你的函数用作

student* deletefirstlist(student*& head, student *nn);

那么就不需要使用间接了。你可以简单地做:

student* deletefirstlist (student* &head, student *nn) {
nn = head;
if (head) {
    head = head->next; // *head->next meant *(head->next)
    nn->next=NULL;
}

return nn;
}

并调用:

node = deletefirstlist(head, node);

您也需要更改 fn 声明:

student* deletefirstlist(student *&head, student *nn);

【讨论】:

    猜你喜欢
    • 2014-04-04
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2021-08-04
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多