您的位置:首页 > 其它

链表-删除指定元素 SDUT 1464

2014-03-25 23:04 232 查看
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
    int data;
    struct node *next;
};

int main()
{
    int n,i,min,max,t;
    scanf("%d",&t);
    while(t--)
    {

    struct node *head,*tail,*p,*a,*b;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;

    scanf("%d%d%d",&n,&min,&max);
    for(i=1;i<=n;i++)
    {
         p=(struct node *)malloc(sizeof(struct node));
         p->next=NULL;
         scanf("%d",&p->data);
         tail->next=p;
           tail=p;

    }

          a=head;
          b=head->next;
          while(b)
          {
              if(b->data>=min && b->data<=max)
              {
                  a->next=b->next;
                  free(b);
                  b=a->next;
              }
              else
              {
                  a=a->next;
                  b=b->next;
              }

           }

   if(head->next==NULL)
    printf("-1\n");
   else
   {
    while(head->next->next)
    {
        printf("%d ",head->next->data);
        head=head->next;
    }
      printf("%d\n",head->next->data);

   }
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: