fork download
// #include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
/*

*/
struct node{
    int data ;
    node *nxt;

    node(int d , node*n=0){
        data = d;
        nxt = n;
    }
};

class list{
    node *head , *tail;

    public : 
    list();
    void add_end(int el);    
    bool is_empty();
    void del_pos(int pos);
    int size();
    void deleteNodesGreaterThan(int el);
    void print();

    

};
void list::print(){
    node *t = head;
    while(t!=0){
        cout<<t->data<<" ";
        t =t->nxt;        
    }
    cout<<endl;
}
void list::deleteNodesGreaterThan(int el){
    int i = 1;
    node *tmp = head;
    while(tmp != 0){
        if(tmp->data > el){
            del_pos(i);
        }
        else {
            i++;
            tmp = tmp->nxt; // Move to the next node only when not deleting
        }
    }
}

int list::size(){
    int c=0;
    node *tmp=head;
    while(tmp!=0){
        c++;
        tmp = tmp->nxt;
    }
    return c;
}
list::list(){
    head = tail = 0;
}
bool list::is_empty(){
    return head==0;
}
void list::add_end(int el){   
    if(is_empty()){
        head = tail= new node(el);
    }
    else {
        tail->nxt= new node(el);
        tail = tail->nxt;
    }


}
void list::del_pos(int pos){
    if(size()==1){
        delete head;
        head = tail = 0;
    }
    else if(pos==1){
        node *tmp = head;
        head = head->nxt;
        delete tmp;
    }
    else if(pos==size()){
        node *tmp = head;
        int i =0;
        while(tmp->nxt!=tail){
            tmp = tmp->nxt;
        }
        delete tail;
        tail = tmp;
        tail->nxt =0;  
    }
    else {
        node *t1 = head , *t2 , *t3;
        for(int i =1;i<pos-1;i++){
            t1=t1->nxt;
        }
        t2= t1->nxt;
        t3=t2->nxt;
        delete t2;
        t1->nxt = t3;
    }

    
}

int main() {
    list l;
    l.add_end(10);
    l.add_end(5);
    l.add_end(-10);
    l.add_end(3);
    l.add_end(4);
    l.add_end(0);
    l.add_end(9);
    l.print();
    l.deleteNodesGreaterThan(5);
    l.print();
}
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
10 5 -10 3 4 0 9 
5 -10 3 4 0 9