单链表删除某区间的值

来源:互联网 发布:vue.js html模板 编辑:程序博客网 时间:2024/05/29 08:56
#include <iostream>//#include"head.h"#include<string.h>using namespace std;struct node{    int data;    node* next;};node* head;int len;void creat(){    head=NULL;    node* r=NULL;    int x;    for(int i=0;i<len;i++)    {        cin>>x;        node* p=new node;        p->data=x;        if(head==NULL)        {            head=p;        }        else        {            r->next=p;        }        r=p;    }    r->next=NULL;    node* newhead=new node;    newhead->next=head;    head=newhead;}int remove( const int& left, const int& right ){    node* cur=head->next;    node* pre=head;    int count=0;    while(cur)    {        if(cur->data>=left&&cur->data<=right)        {            count++;            node* q=cur;            pre->next=cur->next;            cur=cur->next;            delete q;        }        else{             pre=cur;       cur=cur->next;        }    }    return count;}void show_all(){    node* p=head->next;    while(p)    {        cout<<p->data;          if(p->next)            cout<<' ';      p=p->next;    }    cout<<endl;}void discard(){    node* p=head->next;    while(p)    {        node* q=p;        p=p->next;        delete q;    }}int main(){    int T ;    cin>>T;    while(T--)    {        int left;        int right;        cin>>left>>right>>len;        creat();        int ret=remove(left,right);        if(ret!=len)        {            cout<<len-ret<<endl;            show_all();        discard();        }    }    return 0;}

阅读全文
0 0