zoukankan      html  css  js  c++  java
  • C++模板--List容器

    //构造
    //析构
    //拷贝构造
    //赋值构造
    //=
    //<<
    //clear
    //back
    //empty
    //front
    //merge
    //push_front
    //push_back
    //pop_front
    //pop_back
    //remove
    //swap
    //sort
    //size
    //reverse

    template<typename T>
    class List{
        class Node{
        public:
            Node(const T& data, Node* prev = NULL, Node* next = NULL):m_data(data),m_prev(prev),m_next(next){}
            friend ostream& operator<<(ostream& os, const Node& node){
                return os << "["<< node.m_data << "]";
            }
            T m_data;
            Node* m_prev;
            Node* m_next;
        };
        
        Node* m_head;
        Node* m_tail;
     
    public:
    
        List():m_head(NULL), m_tail(NULL){}
        ~List(){
            clear();
        }
     
        List(const List& that):m_head(NULL), m_tail(NULL){
            for(Node* node = that.m_head; node; node = node->m_next){
                this->push_back(node->m_data);
            }
        }
        List& operator=(const List& that){
            if(this != &that){
                List temp = that;
                swap(m_head, temp.m_head);
                swap(m_tail, temp.m_tail);
            }
            return *this;
        }
     
        void clear(void){
            for(Node* node = m_head, *next; node; node = next){
                next = node->m_next;
                delete node;
            }
            m_head = NULL;
            m_tail = NULL;
        }
     
        bool empty(void) const {
            return !m_head && !m_tail;
        }
     
        size_t size(void)const{
            size_t count = 0;
            for(Node* node = m_head; node; node = node->m_next){
                count++;
            }
            return count;
        }
     
        T& front(void){
            if(empty()){
                throw underflow_error("list under flow");
            }
            return m_head->m_data;
        }
     
        const T& front(void) const{
            return const_cast<List*>(this)->front();
        }
     
        void push_front(const T& data){
            m_head = new Node(data, NULL, m_head);
            if(m_head->m_next){
                m_head->m_next->m_prev = m_head;
            }else{
                m_tail = m_head;
            }
        }
     
        void pop_front(void){
            if(empty()){
                throw underflow_error("list under flow");
            }
            Node* temp =  m_head; 
            m_head = m_head->m_next;
            delete temp;
            temp = NULL;
            if(m_head){
                m_head->m_prev = NULL;
            }else{
                m_tail = NULL;
            }
        }
     
        T& back(void){
            if(empty()){
                throw underflow_error("list under flow");
            }
            return m_tail->m_data;
        }
        const T& back(void) const {
            return const_cast<List*>(this)->back();
        }
     
        void push_back(const T& data){
            m_tail = new Node(data, m_tail, NULL);
            if(m_tail->m_prev){
                m_tail->m_prev->m_next = m_tail;
            }else{
                m_head = m_tail;
            }
        }
     
        void pop_back(void){
            if(empty()){
                throw underflow_error("list under flow");
            }
            Node* temp = m_tail;
            m_tail = m_tail->m_prev;
            delete temp;
            temp = NULL;
            if(m_tail){
                m_tail->m_next = NULL;
            }else{
                m_head = NULL;
            }
     
            
        }
     
        void remove(const T& data){
            for(Node* node = m_head, *next; node; node = next){
                next = node->m_next;
                if(data == node->m_data){
                    if(node->m_prev){
                        node->m_prev->m_next = node->m_next;
                    }else{
                        m_head = node->m_next;
                    }
                    if(node->m_next){
                        node->m_next->m_prev = node->m_prev;
                    }else{
                        m_tail = node->m_prev;
                    }
                    delete node;
                }
            }
        }
     
        friend ostream& operator<<(ostream& os, const List& list)
        {
            for(Node* node = list.m_head; node; node = node->m_next)
            {
                os << *node;    
            }
            return os;
        }
    };
  • 相关阅读:
    毕业五年后的差距
    基于jsonlib.jar包Json程序 实战篇
    Hashtable
    [转]浏览器是怎样工作的:渲染引擎,HTML解析
    Web.config之连接字介绍
    jQuery选择器全解【转】
    JavaScript document属性和方法
    zoj 2316 Matrix Multiplication 夜
    zoj 2318 Get Out! 夜
    hdu 3666 THE MATRIX PROBLEM 夜
  • 原文地址:https://www.cnblogs.com/xkk956227639/p/9540133.html
Copyright © 2011-2022 走看看