一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起,给出一个age, 在些链表中删除学生年龄等于age的学生信息。
typedef struct stu
{
char name[20];
char sex;
int no;
int age;
struct stu * next;
}node;
node *create(int n)
{
int i;
node *head,*p ,*s ,*s1;
head = (node*)malloc(sizeof(node));
head->next = NULL;
p = head;
while(n)
{
s = (node*)malloc(sizeof(node));
p->next = s;
printf("请输入每个人的信息:姓名 性别 学号 年龄 ");
scanf("%s %c %d %d",s->name ,&s->sex ,&s->no,&s->age);
s->next = NULL;
p = s;
n--;
}
return head;
}
void del(node *head , int a)
{
node *p,*s,*str;
p = head;
while(p != NULL)
{
s = p;
p = p->next;
if( p->age == a )
{
s->next = p->next;
break;
}
}
}
void display(node *head)
{
node *p;
p = head->next;
while(p != NULL)
{
printf("%s %c %d %d",p->name ,p->sex ,p->no ,p->age);
p = p->next;
}
}
void main()
{
node *head;
int n;
printf("请输入创建的个数:");
scanf("%d",&n);
head = create(n);
display(head);
int age;
printf("请输入删除的年龄:");
scanf("%d",&age);
del(head , age);
display(head);
}