/*建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表
中的结点所包含的年龄等于此年龄,则将此结点删去。*/
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
long num;
char name[10];
char gender[2];
int age;
struct student *next;
}student,*Student;
//创建链表
Student creat()
{
Student s;
int n = 0;
Student p1,p2;
s = p1 = p2 = (Student) malloc(sizeof(student));
printf("请输入:\n");
scanf("%ld %s %s %d",&p1->num,&p1->name,&p1->gender,&p1->age);
while(p1->num != 0)
{
n = n + 1;
if(n == 1) s = p1;
else p2->next = p1;
p2 = p1;
p1 = (Student) malloc(sizeof(student));
scanf("%ld %s %s %d",&p1->num,&p1->name,&p1->gender,&p1->age);
}
p2->next = NULL;
return s;
}
//输出链表
void print(Student s)
{
Student p;
printf("\nNow,The records are:\n");
p = s->next;
if(s != NULL)
{
do
{
printf("%ld %s %s %d\n",p->num,p->name,p->gender,p->age);
p = p->next;
}while(p != NULL);
}
}
int delect(Student a)
{
Student p,q;
p = a;
int age;
printf("请输入年龄:");
scanf("%d",&age);
while(p->next != NULL)
{
q = p->next;
if(q->age == age)
{
p->next = q->next;
break;
}
p = p->next;
}
}
int main()
{
Student a;
a = (Student)malloc(sizeof(student));
a->next = creat();
print(a);
delect(a);
print(a);
}
谭浩强C程序设计(第四版)p330第12题