C++容器List的简易实现

 

template <typename Object>
class List {
private:
    struct Node {
        Object data;
        Node* prev;
        Node* next;

        Node(const Object& d = Object(), Node* p = NULL,
            Node* n = NULL) :data(d), prev(p), next(n) {}
    };
public:
    class const_iterator {
    public:
        const_iterator():current(NULL){}
        const Object& operator* ()const {
            return retrieve();
        }
        const_iterator& operator++() {
            current = current->next;
            return *this;
        }
        const_iterator& operator++(int) {
            const_iterator old = *this;
            ++(*this);
            return old;
        }
        const_iterator& operator--() {
            current = current->prev;
            return *this;
        }
        const_iterator& operator--(int) {
            const_iterator old = *this;
            --(*this);
            return old;
        }
        bool operator==(const const_iterator& rhs) const{
            return current == rhs.current;
        }
        bool operator!=(const const_iterator& rhs) const{
            return !(*this == rhs);
        }
    protected:
        Node* current;
        Object& retrieve() const {
            return current->data;
        }
        const_iterator(Node *p):current(p){}
        friend class List<Object>;
    };
    class iterator :public const_iterator {
    public:
        iterator() {}
        Object& operator* () const {
            return const_iterator::retrieve();
        }
        iterator& operator++() {
            const_iterator::current = const_iterator::current->next;
            return *this;
        }
        iterator& operator++(int) {
            iterator old = *this;
            ++(*this);
            return old;
        }
        iterator& operator--() {
            const_iterator::current = const_iterator::current->prev;
            return *this;
        }
        iterator& operator--(int) {
            iterator old = *this;
            --(*this);
            return old;
        }
    protected:
        iterator(Node* p):const_iterator(p){}
        friend class List<Object>;
    };
public:
    List() {
        init();
    }
    List(const List& rhs) {
        init();
        *this = rhs;
    }
    ~List() {
        clear();
        delete head;
        delete tail;
    }
    void operator=(const List& rhs) {
        if (this != &rhs) {
            clear();
            for (const_iterator iter = rhs.begin(); iter != rhs.end(); iter++) {
                push_back(*iter);
                std::cout << "iter:" <<*iter<< std::endl;
            }
        }
    }

    iterator end() const{
        return iterator(tail);
    }
    iterator begin() const{
        return iterator(head->next);
    }

    int size() const {
        return theSize;
    }
    bool empty() const {
        return theSize == 0;
    }
    void clear(){
        while (!empty())
        pop_front();
    }
    Object& front() {
        return *begin();
    }
    const Object& front() const {
        return *begin();
    }
    Object& back() {
        return *--end();
    }
    const Object& back() const {
        return *--end();
    }
    void push_back(const Object& x) {
        insert(end(), x);
    }
    void push_front(const Object& x) {
        insert(begin(), x);
    }

    void pop_front() {
        erase(begin());
    }
    void pop_back() {
        erase(--end());
    }
    iterator insert(iterator iter, const Object& x) {
        theSize++;
        Node* temp = iter.current;
        std::cout << 123 << std::endl;
        temp->prev->next = new Node(x, temp->prev, temp);
        temp->prev = temp->prev->next;
        std::cout << 123 << std::endl;
        return iterator(temp->prev);
    }
    iterator erase(iterator iter) {
        theSize--;
        Node* current = iter.current;
        iterator retr(current->next);
        current->prev->next = current->next;
        current->next->prev = current->prev;
        return retr;
    }
    iterator erase(iterator start, iterator end) {
        iterator iter = start;
        while (iter != end) {
            iter = erase(iter);
        }
        return end;
    }
private:
    int theSize;
    Node* head;
    Node* tail;
    void init() {
        theSize = 0;
        head = new Node;
        tail = new Node;
        head->next = tail;
        tail->prev = head;
    }
};

  

原文链接: https://www.cnblogs.com/airfy/p/12655719.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++容器List的简易实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/340462

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午12:37
下一篇 2023年3月2日 上午12:37

相关推荐