链表删除出错,报错原因 好像类型转换出错,但是定义的是同一类型呀,求大神指点
程序代码:
出错在删除链表那一行main函数中的这一句错误head=del(head,del_num);错误原因error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct student' (or there is no acceptable conversion) #include<stdio.h> #include<malloc.h> #define NULL 0 struct student { int num; char name[10]; float score[3]; struct student *next; }; typedef struct student stu; void main() { stu *creat(int a);//创建节点数为a的链表; stu *insert(stu *head,stu *add);//head为原链表头指针,add为指向待插入已知链表指针; void print(stu *head);//输出链表 stu del(stu *head,int element);//删除链表 stu *head,*p;int n=2,del_num=1; head=creat(n); print(head); p=(stu*)malloc(sizeof(stu)); printf("******输入学生信息*********\n"); scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]); head=insert(head,p); print(head); head=del(head,del_num); print(head); } stu *creat(int a)//创建节点数为a的链表; { stu *head,*p,*q;int n=0; p=q=(stu*)malloc(sizeof(stu)); printf("******输入学生信息*********\n"); scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]); head=NULL; while(n<a) { n++; if(n==1) head=p; else q->next=p; q=p; p=(stu*)malloc(sizeof(stu)); printf("******输入学生信息*********\n"); scanf("%d%s%f%f%f",&p->num,p->name,&p->score[0],&p->score[1],&p->score[2]); } q->next=NULL; return head; } stu *insert(stu *head,stu *add)//head为原链表头指针,add为指向待插入已知链表指针; { stu *p,*q,*p0; p=q=head;p0=add; if(p==NULL) { head=p0;p0->next=NULL; } else { while(p->num<p0->num&&p->next!=NULL) { q=p;p=p->next; } if(p0->num<=p->num) { if(p==head) {head=p0;p0->next=p;} else { if(p->next==NULL) {p->next=p0;p0->next=NULL;} else {q->next=p0;p0->next=p;} } } } return head; } void print(stu *head)//输出链表 { stu *p; p=head; printf("******输出链表*********\n"); while(p!=NULL) { printf(" num=%d name=%s score[0]=%f score[1]=%f score[2]=%f next=%d\n",p->num,p->name,p->score[0],p->score[1],p->score[2],p->next); p=p->next; } printf("******输出结束*********\n"); } stu *del(stu *head,int element)//删除链表 { stu *p,*q; if(head==NULL) printf(" 链表不纯在 \n"); p=q=head; while(p->num!=element&&p!=NULL) {q=p;p=p->next;} if(p->num==element) { if(p==head)head=p->next; else q->next=p->next; } else printf(" not be found the num \n"); return head; }