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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

模板实现单向链表

發布時間:2025/7/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模板实现单向链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/************************************WZ?ASUST???2016模板實現單向鏈表************************************/#include"sts.h" template?<class?T> struct?node { public:node(const?T?&d):next(NULL),data(d){} T?data; node<T>?*next; }; template?<class?T> class?dlist { private:?node<T>*?head;node<T>*?tail; public:dlist():head(NULL),tail(NULL){};~dlist(){node<T>*cur=head;while(cur){node<T>*del=cur;cur=cur->next;delete?del;}}void?print(){node<T>*cur=head;while(cur){cout<<cur->data<<?"?";cur=cur->next;}cout<<"ok"<<endl;}dlist?&?operator=(const?dlist?<T>?&?dl) { if(this!=&dl){head=dl.head;tail=dl.tail;node<T>*dlcur=dl.head;node<T>*cur=dlcur; int?xx=length()-1;while(xx--){dlcur=dlcur->next;dlcur=cur;cur=cur->next;}}} int?length() { int?ret=0; if(head) {node<T>*cur=head; while(cur) { ret++;cur=cur->next; } return?ret; } else?return?0; }void?pushback(const?T?&d) { if(head==NULL){head=new?node<T>(d);tail=head;} else{tail->next=new?node<T>(d);tail=tail->next;} } T?popback() { if(head==NULL){return?0;} else{return?tail->data;}} void?pushfront(const?T?&d) { if(head==NULL){head=new?node<T>(d);tail=head;} else{node<T>?*add=new?node<T>(d);add->next=head;head=add;} } T?popfront() { if(head==NULL){return?0;} else{return?head->data;}} void?insert(int?x,?const?T?d) { if(x<0||x<length()){x=x-1;node<T>?*add=new?node<T>(d);node<T>?*cur=head;if(head){while(x--){cur=cur->next;}add->next=cur->next;cur->next=add;}} } void?reverse() {int?len=length();node<T>?*tmp=NULL; if(head){node<T>*?p?=?head;node<T>*?q?=?head;??node<T>*?th=NULL;while(len--){q=p;p=p->next;q->next=th;th=q;}head=th; }elsereturn;} void?paixu() {int?len=length();int?state=1;T??tmp; //len>1;while(len--&&state){node<T>*?p?=?head;?node<T>*?q=p->next;while(q){if(p->data?>=?q->data){?tmp=q->data;q->data=p->data;p->data=tmp;?}else?state=1;p=p->next;?????q=q->next;}} } };void?test() { dlist<int>?int_dlist; cout<<"**?1?:?***************"<<endl;int_dlist.pushback(11);int_dlist.pushback(22);int_dlist.pushback(33);int_dlist.pushback(44);int_dlist.print(); cout<<"***?2?:**************"<<endl;int_dlist.pushfront(55);int_dlist.pushfront(66);int_dlist.pushfront(77);int_dlist.print(); cout<<"***?3?:**************"<<endl;cout<<int_dlist.popfront()<<endl;cout<<int_dlist.popback()<<endl; cout<<"***?4?:**************"<<endl;dlist<int>?int_dlist2; int_dlist2=int_dlist;int_dlist.print(); cout<<"**?5?:?***************"<<endl; cout<<int_dlist.length()<<endl;} int?main() { //?test(); dlist<int>?int_dlist;int_dlist.pushback(11);int_dlist.pushback(22);int_dlist.pushback(44);int_dlist.pushback(33);int_dlist.print();cout<<"**?6?:?***************"<<endl; int_dlist.reverse(); int_dlist.print(); cout<<"**?7?:?***************"<<endl; int_dlist.insert(3,99); int_dlist.paixu(); int_dlist.print();return?0; }

轉載于:https://blog.51cto.com/sts609/1758672

總結

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

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