# include <stdio.h> # include <malloc.h> struct Student { char name[100]; int age; struct Student * pNext; }; struct Student * Creat_list(); void Out_list(struct Student * pHead); int main (void){ struct Student * pHead; pHead = Creat_list(); Out_list(pHead); return 0; } struct Student * Creat_list(){ struct Student *pTail = NULL; int len; printf("请输入学生人数:"); scanf("%d",&len); struct Student * pHead = (struct Student * )malloc(sizeof(struct Student) ); if(pHead->pNext == NULL || len == 0){ printf("录入失败,请重新录入!"); } // 为什么不是放在上面 if 里面 ???? pHead->pNext = NULL; pTail = pHead; for (int i = 0; i < len; ++i){ printf("第%d位学生信息:\n",i+1); struct Student * pNew = (struct Student * ) malloc(sizeof(struct Student)); printf("姓名:"); scanf("%s", pNew->name); printf("年龄:"); scanf("%d",&(pNew->age)); pTail->pNext = pNew; pNew->pNext = NULL; pTail = pNew; } return pHead; } void Out_list(struct Student * pHead){ // q == pNew struct Student * q = pHead->pNext; if(q != NULL) printf("学生信息如下:\n"); while (q != NULL){ printf("姓名:%s 年龄:%d\n",q->name, q->age); q = q->pNext; } }
数据结构//C——动态分配—离散存储(非循环单链表)
于 2022-01-16 15:02:59 首次发布