日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

模拟STL链表类的实现

發布時間:2025/5/22 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟STL链表类的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模擬STL鏈表類的實現

STL內部定義了多種容器和迭代器,方便了數據結構類的使用,且不需關注內部源碼。為了方便個人使用習慣,我又重寫了一個鏈表類,作為學C++后的第一個項目作業。我將其命名為clist。

代碼及注釋

cpp /*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;}};

總結

以上是生活随笔為你收集整理的模拟STL链表类的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。