【发布时间】: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->next-->(*head)->next -
我试着做 *head = nn-> next;
-
,它奏效了。对吗?
-
在这种情况下
(*head)->next和nn-> next是相同的。
标签: c linked-list