• 谭浩强第四版第九章课后习题12>>>建立一个链表,每个节点包括:学号、姓名、性别、年龄。输入一个年龄,若链表 中的结点所包含的年龄等于此年龄,则删除此结点。


     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define N sizeof(link)
     4 typedef struct lin
     5 {
     6     struct lin *next;
     7     int num;
     8     char name[20];
     9     char sex;
    10     int age;
    11 }link;
    12 void print(link *head)
    13 {
    14     link *p;
    15     p=head;
    16     printf("打印数据如下:
    ");
    17     while(p)
    18     {
    19         printf("%d	%s	%c	%d
    ",p->num,p->name,p->sex,p->age);
    20         p=p->next;
    21     }
    22 }
    23 int n;
    24 link *creat(void)
    25 {
    26     link *head,*p,*s;
    27     p=s=(link *)malloc(N);
    28     printf("请输入输数据:
    ");
    29     scanf("%d %s %c %d",&p->num,p->name,&p->sex,&p->age);
    30     head=p;
    31     n=0;
    32     while(p->num!=0)
    33     {
    34         n++;
    35         if(n!=1)s->next=p;
    36         s=p;
    37         p=(link*)malloc(N);
    38         printf("请输入数据1:
    ");
    39         scanf("%d %s %c %d",&p->num,p->name,&p->sex,&p->age);
    40     }
    41     s->next=NULL;
    42     return(head);
    43 }
    44 link *del(link *head,int a)
    45 {
    46     link *p,*p1,*s;
    47     for(p=p1=head;p;)
    48     {
    49         if(p->age==a)
    50             if(p==head)    
    51             {
    52                 s=p;
    53                 head=p=p->next;
    54                 free(s);
    55             }
    56             else 
    57             {
    58                 p1->next=p->next;
    59                 s=p;
    60                 p=p->next;
    61                 free(s);
    62 
    63             }
    64         else 
    65             {
    66                 p1=p;
    67                 p=p->next;
    68             }
    69     }
    70     return(head);
    71 }
    72 int main()
    73 {
    74     link *head,*p;
    75     int a;
    76     head=creat();
    77     printf("打印处理前的数据:
    ");
    78     print(head);
    79     printf("请输入年龄:
    ");
    80     scanf("%d",&a);
    81     p=del(head,a);
    82     printf("打印处理后的数据: 
    ");
    83     print(p);
    84     return 0;
    85 }

    测试结果:

    请输入输数据:
    101 Li F 15
    请输入数据1:
    102 Wang M 18
    请输入数据1:
    103 Sun F 19
    请输入数据1:
    104 CH M 15
    请输入数据1:
    105 QIn F 20
    请输入数据1:
    106 Kun F 21
    请输入数据1:
    107 Jun M 15
    请输入数据1:
    108 Xun F 22
    请输入数据1:
    109 Liu M 15
    请输入数据1:
    0 0 0 0
    打印处理前的数据:
    打印数据如下:
    101     Li      F       15
    102     Wang    M       18
    103     Sun     F       19
    104     CH      M       15
    105     QIn     F       20
    106     Kun     F       21
    107     Jun     M       15
    108     Xun     F       22
    109     Liu     M       15
    请输入年龄:
    15
    打印处理后的数据:
    打印数据如下:
    102     Wang    M       18
    103     Sun     F       19
    105     QIn     F       20
    106     Kun     F       21
    108     Xun     F       22
    Press any key to continue
  • 相关阅读:
    iframe与动作连处理
    selenium其他自动化操作
    使用seleniun模拟登陆qq空间
    selenium基本使用
    验证码识别 云打码之古诗文网验证识别
    图片爬取基础
    centos8下LAMP搭建Nextcloud
    浅谈centos8与centos7
    DHCP服务器配置及测试
    使用Apache服务器实现Nginx反向代理
  • 原文地址:https://www.cnblogs.com/happyfei/p/10684604.html
Copyright © 2020-2023  润新知