C语言链表程序求改正!我想保留里面所有功能,但是有错误,求帮忙改正确啊

2024-11-25 12:39:38
推荐回答(2个)
回答1:

#include
#include

#define LEN sizeof(struct student)

struct student {
long num;//学生编号
double score;//学生成绩
struct student *next;
};

struct student *Create(int n) {
int i;
struct student *head, *p;
if(n <= 0) return NULL;
head = p = (struct student *)malloc(LEN);
printf("学生1的学号 : ");
scanf("%ld",&head->num);
printf("学生1的成绩 : ");
scanf("%lf",&head->score);
for(i = 1; i < n; ++i) {
p->next = (struct student *)malloc(LEN);
printf("学生%d/%d的学号 : ",i + 1,n);
scanf("%ld",&p->next->num);
printf("学生%d/%d的成绩 : ",i + 1,n);
scanf("%lf",&p->next->score);
p = p->next;
}
p->next = NULL;
return head;
}

int Change(struct student *head) {
struct student *r;
long num;
int score;
printf("输入一个要修改的学生的学号:");
scanf("%ld",&num);
for(r = head; r; r = r->next) {
if(r->num == num) {
printf("请输入新的成绩:");
scanf("%d",&r->score);
return 1;
}
}
printf("输入学号有误。\n");
return 0;
}

void Print(struct student *head) {
struct student *r;
printf("学号\t成绩\n");
for(r = head; r; r = r->next)
printf("%ld\t%d\n",r->num,r->score);
}

int Find(struct student *head) {
struct student *r;
long num,y;
printf("请输入需要查询成绩的学生学号 : ");
scanf("%d",&num);
for(r = head; r; r = r->next) {
if(r->num == num) {
printf("学号 : %ld\t成绩 : %lf\n",r->num,r->score);
return 1;
}
}
printf("没有找到学号为 : %ld的学生。\n",num);
return 0;
}

int Del(struct student *&head) {
struct student *p1,*p2;
long num;
printf("\n请输入待删除学生的学号:");
scanf("%ld",&num);
if(head->num == num) {
p1 = head;
head = p1->next;
free(p1);
return 1;
}
for(p1 = head; p1->next; p1 = p1->next) {
if(num == p1->next->num) {
p2 = p1->next;
p1->next = p2->next;
free(p1);
return 1;
}
}
printf("学号错误,没有该学生信息!\n");
return 0;
}

void Insert(struct student *&head) {
struct student *p,*node;
node = (struct student *)malloc(LEN);
printf("请输入学生学号 : ");
scanf("%ld",&node->num);
printf("请输入学生成绩 : ");
scanf("%lf",&node->score);
if(node->num < head->num) {
node->next = head->next;
head = node;
return;
}
for(p = head; p->next; p = p->next) {
if(p->next->num > node->num) {
node->next = p->next;
p->next = node;
return;
}
}
p->next = node;
node->next=NULL;
}

回答2:

发过来看看吧,我的邮箱:pandeng4632488@163.com