Files
CWithClasses/C/BasicSyntax/Structure/STRUCTURE_test1.cpp
2025-12-31 00:39:23 +08:00

60 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#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;
}