c++ 实现python 的list

c++ 实现python 的list,第1张

好用的c++ 链表

模仿python list的链表实现的,简单版本,方便遍历,增加,删除,更改,插入 字符串。

运行结果:

测试程序




// tow_list.cpp


// #include 
#include "ulist.h" // list 的实现
/*
作者: 广都
qq: 1975767630
微信: wo15985300747

还有一个配套的ustring 请查看我的csdn
广都--编程每日问 qq_49758204
*/



int main(){
    system("chcp 65001>nul");
    ulist first_list;
    cout<<"列表长度"<<first_list.len()<<endl;
    first_list.append("第一个元素");
    cout<<"列表长度"<<first_list.len()<<endl;
    cout<<"列表: "<<first_list<<endl;// 直接打印

    // 批量添加
    first_list.append(4,"d1","d2","d3","d4"); // 批量添加四个元素。
    cout<<"添加四个元素"<<first_list<<endl;


    ulist ser_list;
    ser_list=to_ulist(2,"第二个元素","3");
    cout<<"列表2 "<<ser_list<<endl;


    // 相加
    ulist th_list ;
    th_list=first_list+ser_list;
    cout<<"列表相加"<<th_list<<endl;

    string sum_str= th_list.to_string();
    cout<<sum_str<<endl;

    cout<<"获得元素"<<endl;
    cout<<first_list<<endl;
    cout<<first_list[0]<<endl; // 正获得
    cout<<first_list[-1]<<endl; // 负数获得。
    cout<<first_list[-2]<<endl;


    cout<<"截取元素"<<first_list(0,2)<<endl;
    cout<<"截取元素"<<first_list(-1,-3)<<endl;

    first_list.del(1);
    cout<<first_list<<endl;

    first_list.del(1,3);//
    cout<<"批量删除  "<<first_list<<endl;

    first_list.replace("d4","d4被替换");
    cout<<"替换"<<first_list<<endl;


    first_list.append(1.1); //添加数字,自动专户为字符串
    cout<<"添加数字"<<first_list<<endl;

    first_list.change(1,"d4修改");
    cout<<"修改  "<<first_list<<endl;

    node *re_node=first_list.find("1.1");
    re_node->str="更改我们";
    cout<<"更改"<<first_list<<endl;


    first_list.insert(0,"头部插入");
    cout<<"头部插入 "<<first_list<<endl;

    first_list.insert(-1,"尾部插入");
    cout<<"尾部插入 "<<first_list<<endl;

    first_list.insert(1,"1插入");
    cout<<"1插入 "<<first_list<<endl;


    first_list.insert(-1,"-1插入");
    cout<<"-1插入 "<<first_list<<endl;



    first_list.insert(-2,"-2插入");
    cout<<"-2插入 "<<first_list<<endl;



    first_list.empty();
    cout<<"清空list "<<first_list<<endl;



    // system("pause");
}








头文件 ulist.h

// ulist.h


/*
作者: 广都
qq: 1975767630
微信: wo15985300747

还有一个配套的ustring类 请查看我的csdn
广都--编程每日问 qq_49758204
*/



#include 
# include 
#include 

#include 
using namespace std;


string str_replace(string primary_str,string res,string dx)
{
    // 字符串拼接 要找个高效的做法。
    string dc=primary_str;
    int pos1 = dc.find(res);
    //未找到时,返回值为-1
    if (pos1 != -1) {
        string s1 = dc.substr(0,pos1);
        string s2 = dc.substr(pos1+res.length());;
        dc=s1+dx+s2;
        dc=str_replace(dc,res,dx); // replace str again
    }
    return dc;
}


string show_no_string(string cout_str){
    string res;
    res=str_replace(cout_str,"\n","\n");
    res=str_replace(res,"\t","\t");
    return res;
}

typedef struct node {
    string str;
    node *next,*last;
} node;



class ulist
{
public:
        node *head; //本身不给出值
        //--------------------function_list
        ulist();
        // ~ulist();
        long len(); // ok
        // void list(string str);// ok
        void append(string append_string); // ok
        void append(double number); // ok
        void append(int argNum,...);
        void append(ulist put_list);
        // bool append(int number,...);
        void addhead(string head_string);//ok
        void insert(long number,string insert_string);
        void print(bool sign=true); //ok
        void empty(); // ok
        string operator[](long mc); //ok

        bool operator!=(ulist &put_list);//ok
        bool operator==(ulist &put_list);//ok
        ulist operator()(long start_number,long end_number);

        ulist operator+(ulist serd_list);//ok
        // ulist operator=(ulist serd_list); //ok
        string to_string(void);// ok
        bool in(string in_str);// ok
        bool del(long start_number,long end_number);
        bool del(long number); // ok  0开始   len-1 结束

        void replace(string restr,string new_str);//ok
        node *find(string fingd_str);//ok
        void change(long number,string str);//ok
};

#include 
string to_ustr(double number) {
    std::stringstream ss;
    ss << number;
    std::string asString = ss.str();
    // cout << asString << endl;
    return asString;
}

double to_num(string ax)
{
    return atof(ax.c_str());
}

void ulist::append(double number){
    this->append(to_ustr(number));
}

ulist ulist::operator+(ulist serd_list){
    ulist re_list;//内存泄漏。

    node *p;
    for(p =serd_list.head->next;p!= serd_list.head;p=p->next){
        re_list.append(p->str);
    }

    for(p =this->head->next;p!= this->head;p=p->next){
        re_list.append(p->str);
    }

    return re_list;

    // if(serd_list.head->next=serd_list.head->last){
    //     return *this;
    // }
    // for(node *p =serd_list.head->next;p!= serd_list.head;p=p->next){
    //     this->append(p->str);
    // }
    // return *this;
}

bool ulist::operator!=(ulist &put_list){
    long s1=this->len();
    long s2=put_list.len();

    if (s1!=s2){
        return true;
    }
    string *str_list = new string[s1];
    node *p;
    long mi=0;
    for(p=this->head->next;p !=this->head;p=p->next){
        str_list[mi]=p->str;
        mi=mi+1;
    }


    mi=0;
    for(p=put_list.head->next;p!=put_list.head;p=p->next){
        if(p->str !=str_list[mi]){
            return true;
        }
    }
    return false;
}





bool ulist::operator==(ulist &put_list){
    // 有待优化,应该是算的过程中,顺带得到长度。
    long s1=this->len();
    long s2=put_list.len();
    // cout<<"  "<
    if (s1!=s2){
        return false;
    }
    string *str_list = new string[s1];
    node *p;
    long mi=0;
    for(p=this->head->next;p !=this->head;p=p->next){
        str_list[mi]=p->str;
        mi=mi+1;
    }
    mi=0;
    for(p=put_list.head->next;p!=put_list.head;p=p->next){
        if(p->str !=str_list[mi]){
            return false;
        }
        mi=mi+1;
    }

    delete str_list;
    return true;
}



bool ulist::del(long start_number,long end_number){
    long len=this->len();
    long s1=0,s2=len;
    if(start_number <0){
        s1= len+end_number;
    }
    else{
        s1=start_number;
    }

    if(end_number<0){
        s2=len+end_number;
    }
    else{
        s2=end_number;
    }
    if(s1>=s2){
        long s3=s1;
        s1=s2;
        s2=s3;
    }
    long mi=0;
    node *p=this->head->next;
    node *pd;
    pd=p->next;
    for(p=this->head->next;p!=this->head;p=pd){
        pd=p->next;
        if(mi>=s1){
            if(mi<s2){
                // 删除,替换
                p->last->next= p->next;
                p->next->last =p->last;
                delete p;
                p=NULL;
            }
            else{
                return true;
            }
        }
        mi=mi+1;
    }
    return true;


}

ulist ulist::operator()(long start_number,long end_number){
    // 复制,返回一个链表结构
    ulist re_list;
    // 转化为正的。
    long len=this->len();
    long s1=0,s2=len;
    if(start_number <0){
        s1= len+start_number+1;
    }
    else{
        s1=start_number;
    }
    if(end_number<0){
        s2=len+end_number+1;
    }
    else{
        s2=end_number;
    }

    if(s1>=s2){
        long s3=s1;
        s1=s2;
        s2=s3;
    }

    // cout<
    long mi=0;
    node *p;
    for(p=this->head->next;p!=this->head;p=p->next){
        if(mi>=s1){
            if(mi<s2){
                re_list.append(p->str);
            }
            else{
                return re_list;
            }
        }
        mi=mi+1;
    }
    return re_list;
}

void ulist::append(int argNum,...){
    va_list args;
    va_start(args, argNum);
    for (int i = 0; i < argNum; i++)
    {
        string ddc = va_arg(args, char *);
        this->append(ddc);
    }
    va_end(args);
}

ulist to_ulist(int argNum,...){
    ulist re_list;
    va_list args;
    va_start(args, argNum);
    for (int i = 0; i < argNum; i++){
        string ddc = va_arg(args, char *);
        re_list.append(ddc);
    }
    va_end(args);
    return re_list;
}

ulist::ulist(){
    this->head =new node;
    this->head->next=this->head;
    this->head->last =this->head;
}


long ulist::len(){
    node *head=this->head;
    node *p=head->next;
    long mi=0;
    while(p !=head){
        // printf("1");
        mi=mi+1;
        p=p->next;
    }
    return mi;
}


void ulist::append(ulist put_list){
    for(node *p=put_list.head->next;p!=put_list.head;p=p->next){
        this->append(p->str);
    }
}

string ulist::to_string(void){
    string re_str="";
    for(node *p=this->head->next;p!=this->head;p=p->next){
        re_str=re_str+p->str;
    }
    return re_str;
}

bool ulist::in(string in_str){
    for(node *p=this->head->next;p!=this->head;p=p->next){
        if(in_str==p->str){
            return true;
        }
    }
    return false;
}



void ulist::append(string append_string){
    node *p=new node;
    p->str=append_string;
    node *end_node=this->head->last;
    end_node->next=p;
    p->last =end_node;
    this->head->last=p;
    p->next=this->head;
}



void ulist::insert(long number,string insert_string){
    // 可以使用整数,负数和0来插入。

    if(number==0){
        this->addhead(insert_string);
        return ;
    }

    node *pnow=new node;
    node *p;
    long mi;
    if(number>=0){
        mi=1;
        p=this->head->next;
        while(p!= this->head){
            if(mi==number){
                pnow->str=insert_string;
                node *next_node=p->next;
                p->next = pnow;
                pnow->next =  next_node;
                next_node->last = pnow;
                pnow->last =p;
                return ;
            }
            mi=mi+1;
            p=p->next;
        }
    }
    if(number<0){
        mi=1;
        number=0-number-1;
        p=this->head->last;
        while(p !=this->head){
            // cout<str<
            if(mi==number){
                pnow->str=insert_string;
                node *last_node=p->last;
                last_node->next =pnow;
                pnow->next=p;
                p->last =pnow;
                pnow->last = last_node;
                return ;
            }
            p=p->last;
            mi=mi+1;
        }
    }
    // 当数字太大或者太小,找不到位置的时候,一律插入到尾部
    this->append(insert_string);
}

string ulist::operator[](long number){
    long mi;
    node *p;
    if(number>=0){
        mi=0;
        p=this->head->next;
        while(p!= this->head){
            if(mi==number){
                return p->str;
            }
            mi=mi+1;
            p=p->next;
        }
    }
    if(number<0){
        mi=1;
        number=0-number;
        p=this->head->last;
        while(p !=this->head){
            // cout<str<
            if(mi==number){
                return p->str;
            }
            p=p->last;
            mi=mi+1;
        }
    }

    return "";
}



void ulist::addhead(string head_string){
    node *p=new node;
    p->str=head_string;
    node *next_node=this->head->next;

    this->head->next = p;
    p->next =  next_node;

    next_node->last = p;
    p->last =this->head;
}

void ulist::empty(){
    node *p=this->head->next;
    node *dp=p->next;
    for(node *p=this->head->next;p !=this->head;p=dp){
        dp=dp->next;
        delete p;
    }
    this->head->next=this->head;
    this->head->last =this->head;
}

void ulist::print(bool sign){

    cout<<" ['";
    for(node *p=this->head->next;p !=this->head;p=p->next){
        cout<<p->str<<"',";
    }
    cout<<" ]";
}



ostream& operator<<(ostream& os, const ulist& print_node){
    node *head=print_node.head;
    cout<<" ['";
    for(node *p=print_node.head->next;p!=print_node.head;p=p->next){
        cout<<p->str<<"',";
    }
    cout<<" ]";
    return os;
}

void ulist::change(long number,string str){
    long mi;
    node *p;
    if(number>=0){
        mi=0;
        p=this->head->next;
        while(p!= this->head){
            if(mi==number){
                p->str=str;
            }
            mi=mi+1;
            p=p->next;
        }
    }
    if(number<0){
        mi=1;
        number=0-number;
        p=this->head->last;
        while(p !=this->head){
            // cout<str<
            if(mi==number){
                p->str=str;
            }
            p=p->last;
            mi=mi+1;
        }
    }

}

node *ulist::find(string fingd_str){
    long mi;
    node *p;
    // mi=0;
    p=this->head->next;
    while(p!= this->head){
        if(fingd_str==p->str){
            return p;
        }
        // mi=mi+1;
        p=p->next;
    }

}


bool ulist::del(long number){ // ok  0开始   len-1 结束
    if(number==0){
        return false;
    }
    long mi;
    node *p;
    if(number>0){
        mi=0;
        p=this->head->next;
        while(p!= this->head){
            if(mi==number){
                p->last->next =p->next;
                p->next->last =p->last;
                delete p;
                return true;
            }
            mi=mi+1;
            p=p->next;
        }
    }
    if(number<0){
        mi=1;
        number=0-number;
        p=this->head->last;
        while(p !=this->head){
            if(mi==number){
                p->last->next= p->next;
                p->last->next =p->last;
                delete p;
                return true;
            }
            p=p->last;
            mi=mi+1;
        }
    }

    return false;
}

void ulist::replace(string restr,string new_str){
    node *p;
    p=this->head->next;
    while(p!= this->head){
        if(restr==p->str){
            p->str=new_str;
        }
        p=p->next;
    }

}

#include  // set list
ulist setlist(const ulist put_list){
    ulist re_list;
    if (put_list.head->next==put_list.head->last){
        return re_list;
    }
    map<string,bool> mydict;
    for(node *p=put_list.head->next;p !=put_list.head;p=p->next){
        if(0 == mydict.count(p->str)){
            re_list.append(p->str);
             mydict[p->str]=true;
        }
    }
    return re_list;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存