首先在我的编译器中,有些代码顺序不能错,其次还有一处 看注释#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat(); // 新建
struct student *del(struct student *heat,int n); // 删除
struct student *cha(struct student *heat); // 插入
void print(struct student *heat); // 打印 struct student *heat
void main()
{
struct student *p;
int m;
p=creat();//新建
print(p);
printf("请输入要删除的学生的学号\n\n");//删除
scanf("%d",&m);
print(del(p,m));
printf("结果\n");//插入
print(cha(p));
}
struct student *creat()
{
struct student *heat;
struct student *p,*q;
q=p=(struct student *)malloc(LEN);
heat=NULL;
while(1)
{
int n=0;
n++;
if(n==1)
{
p=heat;
}
else
{
q->next = p;
}
q=p;
printf("请输入学号:");
scanf("%d",&p->num);
printf("请输入成绩:");
scanf("%f",&p->score);
}
return heat;
}
void print(struct student *heat)
{
struct student *p;
printf("输入结果\n");
p=heat;
while(p)
{
printf("学号是%3d 的成绩是%f\n",p->num,p->score);
p->next;
}
}
struct student *del(struct student *heat,int n)
{
struct student *p,*q;
p=heat;
if(p==NULL)
{
printf("无信息可删除\n\n");
}
else
{
while(p->num!=n&&p->next!=NULL)
{
q=p;
p->next;
}
if(n==p->num)
{
if(p==heat)
heat=p->next;
q->next=p->next;
}
printf("删除成功\n\n");
return heat;
}
}
struct student *cha(struct student *heat)
{
struct student *p,*q,*w;
int a;
p=w=(struct student *)malloc(LEN);
p=heat;
if(p==NULL)//判断是否是空表
{
printf("请输入信息\n\n");
printf("请输入学号:");
scanf("%d",p->num);
printf("请输入成绩:");
scanf("%f",p->score);
printf("输入成功\n\n");
}
printf("请输入学号:");
scanf("%d",q->num);
printf("请输入成绩:");
scanf("%f",q->score);
if(q->num>p->num)//插入头
{
q=heat;
p=q->next;
}
else
{
while(q->num <p->num)
{
p->next;
}
if(p->next==NULL)//插在尾
{
p->next=q;
q->next=NULL;
}
w=p->next; //插在中间
p->next=q;
q->next=w;
}
return heat;
}
|