• 删除指定元素


    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1464

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 typedef struct node
     4 {
     5     int data;
     6     struct node *next;
     7 }lb;
     8 lb *creat(int m)//建顺序表
     9 {
    10     int i;
    11     lb *head,*tail,*p;
    12     head=(lb*)malloc(sizeof(lb));//申请空间,开辟结点
    13     head->next=NULL;
    14     tail=head;
    15     for(i=0;i<m;i++)
    16     {
    17         p=(lb*)malloc(sizeof(lb));
    18         scanf("%d",&p->data);
    19         p->next=NULL;
    20         tail->next=p;
    21         tail=p;
    22     }
    23     return head;
    24 }
    25 void del(lb *head,int a, int b)//删除链表中属于[a,b]的数
    26 {
    27     lb *p,*q;
    28     p=head;
    29     while(p->next!=NULL)
    30     {
    31         if(p->next->data>=a&&p->next->data<=b)
    32         {
    33             q=p->next;
    34             p->next=q->next;
    35             free(q);
    36         }
    37         else
    38             p=p->next;
    39     }
    40     p=head;
    41     if(p->next==NULL)
    42         printf("-1\n");
    43     else
    44     {
    45         while(p->next->next!=NULL)
    46         {
    47             printf("%d ",p->next->data);
    48             p=p->next;
    49         }
    50         printf("%d\n",p->next->data);
    51     }
    52 }
    53 int main()
    54 {
    55     int n,a,b,m;
    56     lb *head;
    57     scanf("%d",&n);
    58     while(n--)
    59     {
    60         scanf("%d %d %d",&m,&a,&b);
    61         head=creat(m);
    62         del(head,a,b);
    63     }
    64     return 0;
    65 }

    若你有更好的方法,请留言。相互交流,共同进步!

  • 相关阅读:
    String和StringBuilder和StringBuffer
    多态
    组件
    反向代理
    基础知识
    reflection
    v-model 与 v-bind:model
    tomcat端口占用问题
    socket
    简要概括内存机制
  • 原文地址:https://www.cnblogs.com/sdutmyj/p/2954597.html
Copyright © 2020-2023  润新知