SDUT_OJ_1464-链表-删除指定元素

题目描述

       对于一个给定的线性表,要求删除线性表内的大于等于 min 且小于等于 max 的数,并输出删除后的线性表
要求:必须使用链表做,否则不计成绩!

输入

        输入的第一行为一个正整数 T,表示有 T 组测试数据。
        每组测试数据的第一行为三个整数n、min、max,表示有 n 个数据,删除的范围为[min, max].第二行为 n 个整数代表初始的 n 个数据。

输出

         输出删除数据后的线性表,如果线性表为空则输出-1

示例输入

2
3 1 2
1 2 3
5 2 1
1 1 1 1 1

示例输出

3
1 1 1 1 1

代码

#include <iostream>  
#include <stdlib.h>  
  
using namespace std;  
  
struct node  
{  
    int data;  
    struct node* next;  
};  
  
int a[10000];  
  
int main()  
{  
    ios::sync_with_stdio(false);  
    int t,n,min,max,i;  
    struct node*p,*head,*q,*tail;  
  
    cin>>t;  
    while(t--)  
    {  
        head=(struct node*)malloc(sizeof(struct node));  
        head->next=NULL;  
        tail=head;  
        cin>>n>>min>>max;  
        for(i=0; i<n; i++)  
        {  
            p=(struct node*)malloc(sizeof(struct node));  
            cin>>p->data;  
            p->next=NULL;  
            tail->next=p;  
            tail=p;  
        }  
        p=head;  
        while(p->next!=NULL)  
        {  
            if(p->next->data>=min&&p->next->data<=max)  
            {   
                q=p->next;  
                p->next=q->next;  
                free(q);  
            }  
            else  
            p=p->next;  
        }  
        p=head->next;  
        if(p==NULL)  
            cout<<-1<<endl;  
        else  
        {  
            while(p!=NULL)  
            {  
                cout<<p->data;  
                p=p->next;  
                if(p!=NULL) cout<<' ';  
                else cout<<endl;  
            }  
        }  
    }  
    return 0;  
}  


 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值