1. 程式人生 > >連結串列——刪除指定元素

連結串列——刪除指定元素

#include<iostream>
#include<stdio.h>
using namespace std;
struct Lnode
{
    int data;
    Lnode * next;
};
Lnode * del(Lnode * head,int a, int b)
{
    Lnode * tail;
    tail=head;
    while(head->next)
    {
        if(head->next->data>=a && head->next->data<=b)
        {
           head->next=head->next->next;
        }
        else
        head=head->next;
    }
    return tail;

}
int main()
{
    int t;
    int n,min,max;
    cin>>t;
    while(t--)
    {
       cin>>n>>min>>max;
       Lnode * head, * tail , * liste;
       head=new Lnode;
       head->data=0;
       head->next=NULL;
       tail=head;
       while(n--)
       {
           int a;
           cin>>a;
           liste=new Lnode;
           liste->data=a;
           liste->next=NULL;
           tail->next=liste;
           tail=liste;
       }
       head=del(head,min,max);
       if(head->next==NULL)
       {
           cout<<"-1"<<endl;
       }
       else
       {
           head=head->next;
           while(head->next)
           {
               cout<<head->data<<" ";
               head=head->next;
           }
           cout<<head->data<<endl;
       }
    }

}