两种解法: 1】: 》》单纯链表知识 #include #include typedef struct student { char s_num[10]; char name[15]; int subject1; int subject2; int subject3; struct student* next; }stu; stu* input(stu* head); void print(stu* head); int main() { stu* head = NULL,*p1 = NULL,*p2 = NULL; int n = 0,i = 0; scanf("%d", &n); p1 = head = (stu*)malloc(sizeof(stu)); if (head == NULL) { puts("Fail to memery"); exit(-1); } while (i < n) { p2 = (stu*)malloc(sizeof(stu)); if (p2 == NULL) { puts("Fail to memery"); exit(-1); } else { p2->next = NULL; p1->next = p2; p1 = p2; } i++; } head = input(head); print(head); } stu *input(stu*head) { stu* p = head; while (p->next != NULL) { getchar(); scanf("%s",p->s_num); scanf("%s",p->name); //fgets(p->s_num,sizeof(p->s_num),stdin);//全部接收32 //fgets(p->name,sizeof(p->name), stdin); scanf("%d", &p->subject1); scanf("%d", &p->subject2); scanf("%d", &p->subject3); p = p->next; } return head; } void print(stu* head) { stu* p = head; while (p->next) { printf("%s,%s,%d,%d,%d\n",p->s_num,p->name,p->subject1,p->subject2,p->subject3); p = p->next; } } 2】: #include #include typedef struct student { char s_num[10]; char name[15]; int subject[3]; }stu; void input(stu* head); void print(stu* head); int main() { int n = 0, i = 0; scanf("%d",&n); stu *list = (stu*)malloc(n*sizeof(stu));//此时C自动将其分配好 for(i = 0;i<n;input(&list[i]),i++); for(i = 0;i<n;print(&list[i]),i++); } void input(stu*part) { scanf("%s%s%d%d%d",part->s_num,part->name,&part->subject[0],&part->subject[1],&part->subject[2]); while(getchar()!='\n') continue; } void print(stu* part) { printf("%s,%s,%d,%d,%d\n",part->s_num,part->name,part->subject[0],part->subject[1],part->subject[2]); }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:617 |
C语言程序设计教程(第三版)课后习题9.3 (Java代码)浏览:984 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:916 |
【蟠桃记】 (C语言代码)浏览:677 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:773 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:608 |
1113题解浏览:801 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:581 |
1050题解(结构体数组与结构体指针的使用)浏览:1147 |
钟神赛车 (C语言代码)浏览:618 |