栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++stl list的实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++stl list的实现

template
class List {
private:
    class Node{
    public:
        type e;
        Node *next;
        Node():next(NULL){}
        explicit Node(type data,Node *next=NULL):e(data),next(next){}
    };
    Node * first;
    Node * end;
    long long len{};
public:
    List();
    ~List();
    List (const List  &a);
    void push_back(type e);
    void push_front(type e);
    void pop_back();
    void pop_front();
    type back();
    type front();
    void sort();
    long long size();
    bool empty();
    void clear();
    List& operator = (const List&ri);
    type & search(type e);
    void del(type e);
    void del_all(type e);
};

template
void List::del_all(type e) {
    if(len>2) {
        while (first->e == e) {
            Node *p = first;
            first = first->next;
            delete p;
        }
        Node *p = first, *pp = first->next;
        while (pp != nullptr) {
            bool fl = true;
            if (pp->e == e) {
                p->next = pp->next;
                delete pp;
                fl = false;
                pp = p->next;
            }
            if (fl) {
                p = p->next;
                pp = pp->next;
            }
        }
    }else if (first->e == e)clear();
}

template
void List::del(type e) {
    if(len>2){
        if(first->e == e){
            Node * p = first;
            first = first->next;
            delete p;
        }else{
            Node*p=first,*pp = first->next;
            while(pp!= nullptr){
                if(pp->e == e){
                    p->next=pp->next;
                    delete pp;
                    return;
                }
                p = p->next;
                pp = pp->next;
            }
        }
    }else if (first->e == e)clear();
}

template
type &List::search(type e) {
    Node*p=first;
    while(p!= nullptr){
        if(p->e == e){
            return *p;
        }
        p = p->next;
    }
    return *first;
}

template
void List::pop_back() {

}

template
void List::pop_front() {
    Node*p = first;
    first = first->next;
    delete p;
}

template
void List::clear() {
    if(first!= nullptr){
        Node * p = first;
        while (p!= nullptr){
            Node * pp = p->next;
            p = pp;
        }
    }
}

template
bool List::empty() {
    return len == 0;
}

template
long long List::size() {
    return len;
}

template
void List::push_front(type e) {
    if (this->first!= nullptr){
        this->first = new Node(e, first);
    }
    else {
        this->first = new Node(e, nullptr);
        this->end = this->first;
    }
    len++;
}

template
void List::sort() {
    if(len > 1){
        Node*p = first;
        Node*pp = first->next;
        for (int i = 0; i < len - 1; ++i) {
            for (int j = 0; j < len - 1 - i; ++j) {
                if(pp->ee){
                    type temp = pp->e;
                    pp->e = p->e;
                    p->e = temp;
                }
                p = p->next;
                pp = pp->next;
            }
            p = first;
            pp = first->next;
        }
    }
}

template
List &List::operator=(const List &ri) {
    if(this != &ri){
        if(first != nullptr){
            while(first!= nullptr){
                Node*p = first;
                p = p->next;
                delete first;
                first = p;
            }
            len=0;
        }
        Node*p = ri.first;
        while (p!= nullptr){
            push_back(p->e);
            p = p->next;
        }
    }
    return *this;
}

template
List::List(const List &a) {
    Node*p = a.first;
    first = nullptr;
    end = nullptr;
    while (p!= nullptr){
        push_back(p->e);
        p = p->next;
    }
}

template
type List::front() {
    return first->e;
}

template
type List::back() {
    return end->e;
}

template
List::List() {
    first = nullptr;
    end = nullptr;
    len=0;
}

template
List::~List(){
    clear();
}

template
void List::push_back(type e) {
    if (this->end!= nullptr){
        this->end->next = new Node(e, nullptr);
        this->end = this->end->next;
    }
    else {
        this->first = new Node(e, nullptr);
        this->end = this->first;
    }
    len++;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号