模拟STL链表类的实现

来源:互联网 发布:laydate.js报错 编辑:程序博客网 时间:2024/06/05 15:01

模拟STL链表类的实现

STL内部定义了多种容器和迭代器,方便了数据结构类的使用,且不需关注内部源码。为了方便个人使用习惯,我又重写了一个链表类,作为学C++后的第一个项目作业。我将其命名为clist。

代码及注释

    /*    clist是一个链表类,而_clist是链表的一个单元,iter是一个迭代器(与STL模板库用法相同)。    clist的成员函数:        int:getnum()返回clist中的单元数量。        _clist*:add()在clist结尾添加一个单元,但data为初始值。        _clist*:add(Type x)在clist结尾添加一个单元,且该单元data为x。        _clist*:add(_clist* p,Type x)在p后添加一个单元,且该单元data为x,如果p为NULL则在开头添加。        _clist*:add(_clist* p)在p后添加一个单元但该单元data为初始值。        void:del(clist* p)删除p指向的元素。        void:del()删除整个链表的所有元素。        _clist*:next(_clist* p)返回p的下个单元。        _clist*:prev(_clist* p)返回p的上个单元。        void:putdata(_clist* p,Type x)将x赋给p指向的单元的data。        void:putsymbol(_clist*p,int x)将x赋给p指向的单元的symbol。        T:data(_clist* p)返回p指向的单元的data值。        int:symbol(_clist*p)返回p指向单元的symbol值。        不建议使用未列出的函数。    _clist的变量说明:        _clist* next:该单元的下个单元。        _clist* prev:该单元的上个单元。        T data:该单元的数据值。        int symbol:该单元的标记值。    iter的使用说明:        iter ++:可以使迭代器从现在位置移向下一位置。        iter --:可以使迭代器从现在位置移向上一位置。        _clist<T>* &:返回迭代器现在指向的地址。        void =(_clist<T>* P):使迭代器指向一个单元的地址。        bool ==,!=:判断迭代器的指向地址与另一_clist<T>*地址是否相同。        T *:返回迭代器现在的data值。        void <<T x:将一个值x赋给现在迭代器指向单元的data。    */    #include<stddef.h>    template <typename T>    class _clist{        public:              T data;            _clist* next;            _clist* prev;            int symbol;            _clist(){                next=NULL;                prev=NULL;                symbol=0;            }    };    template <typename T>    class iter{        _clist<T>* thepointer;        public:              T data;              bool eol,bol;            _clist<T>* next;            _clist<T>* prev;            int symbol;            iter(){                next=NULL;                prev=NULL;                symbol=0;                thepointer=NULL;                eol=0;                bol=0;            }            iter<T> operator ++(int){            if (next==NULL) {eol=1;thepointer=NULL;return *this;}            prev=(*next).prev;            symbol=(*next).symbol;            data=(*next).data;            thepointer=next;            next=(*next).next;            return *this;            }            iter<T> operator --(int){            if (prev==NULL) {bol=1;thepointer=NULL;return *this;}            next=(*prev).next;            symbol=(*prev).symbol;            data=(*prev).data;            thepointer=prev;            prev=(*prev).prev;            return *this;            }            bool operator =(_clist<T> *p){                if (p==NULL) return 1;                data=(*p).data;                next=(*p).next;                prev=(*p).prev;                symbol=(*p).symbol;                thepointer=p;                bol=eol=0;                return 0;            }            bool operator ==(_clist<T>* p){                if (p==thepointer) return 1;                else return 0;            }            bool operator !=(_clist<T>* p){                if (p==thepointer) return 0;                else return 1;            }            T operator *(){                return data;            }            _clist<T>* operator &(){                return (thepointer);            }            void operator <<(T x){                (*thepointer).data=x;            }    };    template <typename T>    class clist{    public:        _clist<T> *head,*tail;        _clist<T> *lastp;        int num;            clist(){                head=NULL;                tail=NULL;                num=0;            }            int getnum(){                _clist<T> *pp;                pp=head;                int n=0;                while (pp!=NULL){                    n++;                    pp=(*pp).next;                }                return n;            }            clist(_clist<T>& h){                head=&h;                num=getnum();                _clist<T> *pp=head;                while ((*pp).next!=NULL)                    pp=(*pp).next;                tail=pp;            }        _clist<T>* add(){            _clist<T>* object2=new _clist<T>;            _clist<T>* object1=gettail();            if (object1!=NULL) {                _clist<T>* t;                t=(*object1).next;                (*object1).next=object2;                (*object2).prev=object1;                (*object2).next=t;                if (t!=NULL) (*t).prev=object1;                num=getnum();                if ((*object2).next==NULL) tail=object2;            }            else{                _clist<T>* t=head;                (*object2).next=t;                (*object2).prev=NULL;                if (t!=NULL) (*t).prev=object2;                head=object2;                if (tail==NULL) tail=object2;                num=getnum();            }            return object2;        }        _clist<T>* add(T data){            _clist<T>* object2=new _clist<T>;            (*object2).data=data;            _clist<T>* object1=tail;            if (object1!=NULL) {                _clist<T>* t;                t=(*object1).next;                (*object1).next=object2;                (*object2).prev=object1;                (*object2).next=t;                if (t!=NULL) (*t).prev=object1;                num=getnum();                if ((*object2).next==NULL) tail=object2;            }            else{                _clist<T>* t=head;                (*object2).next=t;                (*object2).prev=NULL;                if (t!=NULL) (*t).prev=object2;                head=object2;                if (tail==NULL) tail=object2;                num=getnum();            }            return object2;        }        _clist<T>* add(iter<T>& object){            _clist<T>* object1=&(object);            _clist<T>* object2=new _clist<T>;            if (object1!=NULL){                _clist<T>* t;                t=(*object1).next;                (*object1).next=object2;                (*object2).prev=object1;                (*object2).next=t;                if (t!=NULL) (*t).prev=object1;                num=getnum();                if ((*object2).next==NULL) tail=object2;            }            else{                _clist<T>* t=head;                (*object2).next=t;                (*object2).prev=NULL;                if (t!=NULL) (*t).prev=object2;                head=object2;                if (tail==NULL) tail=object2;                num=getnum();            }            return object2;        }        _clist<T>* add(iter<T> object,T data){            _clist<T> *object1=&object;            _clist<T>* object2=new _clist<T>;            (*object2).data=data;            if (object1!=NULL){                _clist<T>* t;                t=(*object1).next;                (*object1).next=object2;                (*object2).prev=object1;                (*object2).next=t;                if (t!=NULL) (*t).prev=object1;                num=getnum();                if ((*object2).next==NULL) tail=object2;            }            else{                _clist<T>* t=head;                (*object2).next=t;                (*object2).prev=NULL;                if (t!=NULL) (*t).prev=object2;                head=object2;                if (tail==NULL) tail=object2;                num=getnum();            }            return object2;        }        void del(_clist<T> *object){            if (object==lastp) lastp=NULL;            if (object==head) head=(*object).next;            if (object==tail) tail=(*object).prev;            _clist<T> *i=(*object).prev,*j=(*object).next;            if (i!=NULL) (*i).next=j;            if (j!=NULL) (*j).prev=i;            delete object;        }        void del(){            _clist<T>* p=init();            _clist<T>* i=p;            p=next();            while (p!=NULL){                delete i;                i=p;                p=next();            }            head=tail=NULL;            delete i;        }        _clist<T>* next(_clist<T>* pp){            if (pp==NULL) return NULL;            else {                _clist<T>* hehe=(*pp).next;                return hehe;            }        }        _clist<T>* prev(_clist<T> *pp){            if (pp==NULL) return NULL;            else {                _clist<T>* hehe=(*pp).prev;                return hehe;            }        }        void movep(_clist<T>* p){            lastp=p;        }        _clist<T>* init(){            lastp=gethead();            return lastp;        }        void putdata(_clist<T>*p,T data){            (*p).data=data;        }        void putsymbol(_clist<T>*p,int symbol){            (*p).symbol=symbol;        }        T data(_clist<T>*p){return (*p).data;}        int symbol(_clist<T>*p){return (*p).symbol;}        _clist<T> gethead(){return *head;}        _clist<T> gettail(){return *tail;}    };
0 0
原创粉丝点击