|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求建立一个动态链表来来记录学生的学号和分数,然后输入一个学号,删掉那个学号和分数输出其他的学号和分数
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student{
int num;
float score;
struct Student *next;
};
struct Student*creat(void){
int n = 0;
struct Student *p1,*p2,*head;
head = NULL;
p1 = p2 = (struct Student*) malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
while(p1->num != 0){
n = n + 1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Student*) malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next = NULL;
return (head);
}
int main()
{
struct Student *pt,*p3,*p4;
int a;
pt = creat();
printf("输入一个学生的学号");
scanf("%d",&a);
for(p3 = pt,p4 = NULL;p3->num != a&&p3 != NULL;p4 = p3,p3 = p3->next);
if(p3 == NULL){
printf("没有此学号");
}
if(p4 == NULL){
pt = pt->next ;
free(p3);
while(pt != NULL){
printf("\n%d %f\n",&pt->num,&pt->score);
}
}
if(p3->num == a){
p4->next = p3->next ;
free(p3);
while(pt == NULL){
printf("\n%d %f\n",&pt->num,&pt->score);
}
}
return 0;
} |
-
|