#include #include #define length sizeof(student) union age1{ int age2; char c; }a; struct student{ int num; char name[100]; char sex[100]; union blast{ int age; char f; }category; struct student * next; }; int main(){ int n; scanf("%d",&n); struct student temp,* p,* head,* p2; printf("Enter By NUMBER NAME SEX AGE\n"); p = (struct student *)malloc(sizeof(struct student)); //head = (struct student *)malloc(sizeof(struct student)); //p2 = (struct student *)malloc(sizeof(struct student)); /*for(int i = 0; i < n; i++){ scanf("%d%s%s%d",&a[i].num,a[i].name,a[i].sex,&a[i].age); a[i].next = (struct student *)&a[i]; } 这是直接用struct的数组的形式,这里自动分配了内存存储数据,下面是是动态链表 */ head = p; int i = 1; while(i <= n){ scanf("%d%s%s%d",&p->num,p->name,p->sex,&p->category.age); if(i == n) { p->next = NULL; break; } else p2 = (struct student *)malloc(sizeof(struct student)); p->next = p2; p = p2; i++; } p = head; while(p!=NULL){ printf("The age of %s is %d, he is a %s\n",p->name,p->category.age,p->sex); p2 = p; p = p->next; free(p2); } printf("\n"); //free(p); //free(head); //free(p2); return 0; }