|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 刘帆 于 2021-3-11 10:35 编辑
1、小白一枚,全程跟甲鱼哥。
2、学习到链表时,照着打的,报错 incompatible types - from 'char [24]' to 'struct student *'
3、求修改意见。
4、谢谢大家。
5、代码如下,我把报错的地方标红了。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)//定义student结构的大小
struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert (struct student *head,struct student *stu_2);
void print(struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu,*p,stu_2;
int n;
stu = creat();
p = stu;
print(p);
printf("\nplease input the number to delete:");
scanf("%d",&n);
print(del(p,n));
printf("\nplease input the number to insert:");
scanf("%d",&stu_2.num);
print("please input the score");
scanf("%f",&stu_2.score);
p=insert(stu,&stu_2);
print(p);
printf("\n\n");
system("pause");
}
/*****************************************/
struct student *creat()
(
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("please enter the num");
scanf("%d,"&p1->num);
printf("please enter the score");
scanf("%f,"&p1->score);
head=NULL;
n=0;
while(p1->num!=0)
{
n++;
if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("please enter the num");
scanf("%d,"&p1->num);
printf("please enter the score");
scanf("%f,"&p1->score);
}
p2->next=NULL;
return head;
)
/*****************************************/
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(head == NULL)
{
printf("\nthis list is null!!\n");
goto END;
}
p1=head;
while(p1->num!=num && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num == num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("\nDelete NO:%d succeed!\n",num);
n=n-1;
}
else
{
printf("%d not been found!\n",num);
}
END:
return head;
}
/*****************************************/
struct student *insert (struct student *head,struct student *stu_2)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stu_2;
if(head == NULL)
{
head=p0;
po->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next !=NULL))
{
p2=p1;//删除需要用p1的节点,所以用p2来保存
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head == p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}
/*****************************************/
void print(struct student *head)
{
struct student *p;
printf("\nthere are %d record!\n\n",n);
p=head;
if(head!=0)
{
do
{
printf("number is %d,the garde is %f\n",p->num,p->score);
p=p->next;
}while(p!=0);
}
} |
|