请我尝试删除由值传递并由函数返回的节点,但是头部是通过引用传递的。我尝试了这段代码,但编译器抱怨:* 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;
}
感谢百万
答案 0 :(得分:0)
从引用的代码中我了解到您创建了一个包含头部和链接节点的列表,然后您尝试删除第一个(头部)节点。如果是这样,那么 您的代码必须更正为以下内容:
#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;
}
希望这些帮助。
答案 1 :(得分:0)
整体修正样本
#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;
}
答案 2 :(得分:0)
考虑到编译器抱怨,这将解决它
*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);