1. 程式人生 > >c++ 模版程式設計,構造迭代器和雙向連結串列

c++ 模版程式設計,構造迭代器和雙向連結串列

#pragma once
#include <iostream>
#include <stdexcept>
#include "func.h"
// 連結串列
    template <typename T>class list; //前置宣告,用於宣告友類
    template <typename N>
    class list_iterator{

        N*pos;
        template <typename T> friend  class list; //list 模版可以訪問私有成員進行插入刪除等操作

    public:
        //重新命名各種型別以方便其他模版提取
        typedef typename N::value_type value_type;
        typedef typename N::reference_type reference_type;
        typedef typename N::const_reference_type const_reference_type;
        typedef list_iterator<N> self_type;

        //構造一個空的迭代器
        list_iterator():pos(0){}
        list_iterator(N*pos):pos(pos){}

        bool operator !=(self_type const & right)const{
            return pos !=right.pos;
        }

        bool operator ==(self_type const & right)const{
            return pos!=right.pos;
        }

        self_type &operator++(){
            if(pos) pos=pos->next;
            return *this;
        }
        reference_type operator*()throw (std::runtime_error){
            if(pos) return pos->value;
            else throw (std::runtime_error("dereferencing null iterator"));
        }

    };

//雙向連結串列容器模版
    template <typename T>
    struct list_node{
        typedef T value_type;
        typedef T& reference_type;
        typedef const T& const_reference_type;

        T value;
        list_node *prev;
        list_node *next;
        list_node(T const & value,list_node*prev,list_node*next):value(value),prev(prev),next(next){}
    };
//雙向連結串列類模版
    template <typename T>
    class list{
        typedef list_node<T> node_type;
        node_type*head; //頭節點指標
    public:
    typedef T value_type;
    typedef list_iterator<node_type>iterator;

    list():head(){}
    ~list(){
        //在連結串列系勾時刪除連結串列節點空間
        while (head)
        {
            node_type*n=head;
            head=head->next;
            delete n;
        }

    }

    //從表頭插入資料
    void push_front(T const & v){
        head=new node_type(v,0,head);
        if(head>next){
            head->next->prev=head;
        }
    }
    // 從表頭刪除資料
    void pop_front(T const &v){
        if(head){
            node_type*n =head;
            head=head->next;
            head->prev=0;
            delete n;
        }
    }

    //從指定位置 之後插入資料
    void insert(iterator it,T const &v){
        node_type *n =it.pos;
        if (n){
            node_type*new_node=new node_type(v,n,n->next);
            new_node->next->prev=new_node;
            n->next=new_node;
        }
    }

    //刪除指定元素
    void erase(iterator & it){
        node_type*n =it.pos;
        ++it;
        if(n){
            if(n->next)n->next->prev=n->prev;
            if(n->prev) n->prev->next=n->next;
            if(head==n){
                head=n->next;
            }
            delete n;
        }
    }

    bool is_empty()const{return head==0;}

    //返回指向連結串列頭節點的迭代器
    iterator begin(){
        return iterator(head);
    }
    //空迭代器代表連結串列結尾
    iterator end(){
        return iterator();
    }

};