刘帆 发表于 2021-3-11 10:13:40

求助:小甲鱼C语言教学第57课链表的增加和删除代码问题

本帖最后由 刘帆 于 2021-3-11 10:35 编辑

1、小白一枚,全程跟甲鱼哥。
2、学习到链表时,照着打的,报错 incompatible types - from 'char ' to 'struct student *'
3、求修改意见。
4、谢谢大家。
5、代码如下{:10_266:},我把报错的地方标红了。
#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);
}
}

刘帆 发表于 2021-3-11 10:16:01

本帖最后由 刘帆 于 2021-3-11 10:20 编辑

不能上传图片

刘帆 发表于 2021-3-11 14:18:50

求助求助各位大佬解惑

刘帆 发表于 2021-3-12 16:24:43

{:10_266:}
页: [1]
查看完整版本: 求助:小甲鱼C语言教学第57课链表的增加和删除代码问题