/*
原代码错误太多,以下代码基本上是重写的,请仔细阅读代码。
*/
#include
#include
#include
typedef struct student {
int num;
char name[20];
double score;
struct student *link;
}*linklist;
linklist creatlist(int n) {
linklist llist,p,q;
p = llist = (linklist)malloc(sizeof(struct student));
p->num = 0;
p->name[0] = '\0';
for(int i = 0;i < n;i++) {
q = (linklist)malloc(sizeof(struct student));
printf("第%d/%d学生的学号 : ",i + 1,n);
scanf("%d",&q->num);
printf("第%d/%d学生的姓名 : ",i + 1,n);
scanf("%s",q->name);
printf("第%d/%d学生的成绩 : ",i + 1,n);
scanf("%lf",&q->score);
printf("\n");
p->link = q;
p = q;
}
p->link = NULL;
return llist;
}
void seachlist(linklist llist) {
int n;
linklist p = llist->link;
printf("学号 : ");
scanf("%d",&n);
while((p != NULL) && (p->num != n)) p = p->link;
if(p == NULL) printf("无此学生!\n\n");
else printf("学号 : %d, 姓名 : %s, 成绩 : %6.2lf\n\n",p->num,p->name,p->score);
}
void setnewscore(linklist llist) {
int n;
linklist p;
printf("学号 : ");
scanf("%d",&n);
p = llist->link;
while((p != NULL) && (p->num != n)) p = p->link;
if(p == NULL) printf("无此学生!\n\n");
else {
printf("请输入新成绩 : ");
scanf("%lf",&p->score);
}
}
void deletelink(linklist llist) {
int n;
linklist p,q;
printf("学号 : ");
scanf("%d",&n);
p = llist->link;
while((p != NULL) && (p->num != n)) p = p->link;
if(p == NULL) printf("无此学生!\n\n");
else {
q = p->link;
p = q->link;
free(q);
}
}
void insertpostlink(linklist llist) {
int n;
linklist p,q;
printf("学号 : ");
scanf("%d",&n);
p = llist;
while((p->link != NULL) && (p->link->num != n)) p = p->link;
if(p->link != NULL) printf("该学生已在表中!\n\n");
else {
while((p->link != NULL) && (p->link->num != n)) p = p->link;
q = (linklist)malloc(sizeof(struct student));
printf("姓名 : ");
scanf("%s",q->name);
printf("成绩 : ");
scanf("%lf",&q->score);
q->num = n;
p->link = q;
q->link = NULL;
}
}
void print(linklist llist) {
linklist p = llist->link;
while(p != NULL) {
printf("学号:%d, 姓名:%s, 分数:%6.2lf\n",p->num,p->name,p->score);
p = p->link;
}
}
void whole(linklist llist,int an) {
print(llist);
switch(an) {
case 1:
print(llist);
seachlist(llist);
break;
case 2:
print(llist);
setnewscore(llist);
break;
case 3:
print(llist);
insertpostlink(llist);
break;
case 4:
print(llist);
deletelink(llist);
}
}
void freeheap(linklist head) {
linklist p,q;
p = head;
q = p->link;
while(q != NULL) {
p = q;
q = p->link;
free(p);
}
free(head);
}
int main() {
int an = 0;
linklist llist = creatlist(3);
print(llist);
do {
// system("CLS");
printf("1:查询 2:更改 3:插入 4:删除 5:退出\n");
printf("请输入 : ");
scanf("%d",&an);
whole(llist,an);
}while(an != 5);
freeheap(llist);
return 0;
}