建立一个动态链表,每个节点包括学号,姓名,性别,年龄.

2025-06-29 00:57:07
推荐回答(2个)
回答1:

#include
#include
#define len sizeof(struct student)
struct student
{
int num;
char name[10];
char sex[10];
int age;
struct student *next;
};
void create();
void deletes();
void output();
struct student *head=NULL,*p1,*p2,*p3;
int main()
{
create();
output();
deletes();
output();
return 0;
}
void create()
{
int n,i;
char ch;
printf("请输入要输入的学生信息的条数\n");
scanf("%d",&n);
head=p1=(struct student *)malloc(len);
for(i=0;i {
printf("请输入学生的学号\n");
scanf("%d",&p1->num);
printf("请输入学生的姓名\n");
scanf("%s",p1->name);
ch=getchar();
printf("请输入学生的性别\n");
scanf("%s",p1->sex);
printf("请输入学生的年龄\n");
scanf("%d",&p1->age);
p2=(struct student *)malloc(len); //申请动态空间并初始化
p2->next=NULL;
p1->next=p2;
p3=p1;
p1=p2;
}
p3->next=NULL;
free(p2);
}
void deletes()
{
int ages;
p1=head;
printf("请输入一个年龄\n");
scanf("%d",&ages);
while(ages!=p1->age&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(ages==p1->age)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("删除完毕\n");
}
else printf("没有找到要删除的数据\n");
}
void output()
{
p1=head;
while(p1!=NULL)
{
printf("学号:%d姓名:%s性别:%s年龄:%d\n",p1->num,p1->name,p1->sex,p1->age);
p1=p1->next;
}
}

回答2:

你是要求建立链表还是要求输入年龄,查询相同年龄的信息呢?或者是边建立边查询呢?