建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
 struct student
 {
     int num;
     char name[8];
     char sex;
     int age;
     struct student * next;
 };
 int main()
 {
     struct student * creat();
     struct student * del(struct student *,int );
     void print(struct student * );
     struct student * head,* p;
     int age;
     printf("请输入英雄信息:\n");
     head=creat();
     printf("请输入要删除的英雄年龄:");
     scanf("%d",&age);
     head=del(head,age);
     printf("年龄为%d的英雄已删除,剩下的英雄:\n",age);
     print(head);
     return 0;
 }
 struct student * creat()
 {
     struct student * head, * p1,* p2;
     int n=0;
     p1=p2=(struct student *)malloc(LEN);
     scanf("%d,%s %c,%d",&p1->num,&p1->name,&p1->sex,&p1->age);
     while(p1->num!=0)
     {
         n=n+1;
         if(n==1)head=p1;
         else p2->next=p1;
         p2=p1;
         p1=(struct student *)malloc(LEN);
         scanf("%d,%s %c,%d",&p1->num,&p1->name,&p1->sex,&p1->age);
     }
     p2->next=NULL;
     return head;
 }
 struct student * del(struct student * sh,int ag)
 {
     struct student * p1,* p2;
     p1=p2=sh;
     if(sh==NULL)
     {
         printf("no record");
         return sh;
     }
     else
     {
         do
         {
           while((ag!=p1->age)&&(p1->next!=NULL))
           {p2=p1;
            p1=p1->next;
           }
           if(ag==p1->age)
           {if(p1==sh) 
            {sh=p1->next;}
            else p2->next=p1->next;
            p1=p1->next;
           }
           else if(p1->next==NULL) 
            {p2->next=p1;
             p1=p1->next;
            }
          }while(p1!=NULL);
     }
     return sh;
 }
 void print(struct student * head)
 {
     struct student * p;
     p=head;
     printf("序号  名字  性别 年龄\n");   
     while(p!=NULL)
     {
         printf("%d%8s %2c %5d\n",p->num,p->name,p->sex,p->age);
         p=p->next;
     }
 }

运行结果:

建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。