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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

穷究链表(十三)

發(fā)布時(shí)間:2025/7/14 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 穷究链表(十三) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前面完成了三個(gè)比較完整的鏈表程序。下面就是使用模板類(lèi)來(lái)實(shí)現(xiàn)這個(gè)鏈表程序了。?

Code
??1#include?<iostream>
??2using?namespace?std;
??3
??4template<typename?T>
??5class?linkedlist;
??6
??7template<typename?T>
??8class?listnode
??9{
?10????friend?class?linkedlist<T>;
?11private:
?12????T?data;
?13????listnode<T>?*next;
?14
?15public:
?16????listnode(T?d,listnode<T>?*n=NULL):data(d),next(n)
?17????{
?18????}

?19????
?20????T?getdata()
?21????{
?22????????return?data;
?23????}

?24}
;
?25
?26template<typename?T>
?27class?linkedlist
?28{
?29public:
?30????linkedlist();
?31????~linkedlist();
?32????
?33????void?addNode(listnode<T>?*node,?int?pos);
?34????void?deleteNode(int?pos);
?35????void?reverselist();
?36????void?printlist();
?37????listnode<T>*?findNode(int?pos);
?38private:
?39????void?Destroy();
?40private:
?41????listnode<T>?*header;
?42????int?size;
?43}
;
?44
?45template<typename?T>
?46linkedlist<T>::linkedlist()
?47{
?48????header=NULL;
?49????size=0;
?50}

?51
?52template<typename?T>
?53linkedlist<T>::~linkedlist()
?54{
?55????if(header)
?56????????Destroy();
?57????header=NULL;
?58????size=0;
?59}

?60
?61template<typename?T>
?62void?linkedlist<T>::Destroy()
?63{
?64????listnode<T>*?p=header;
?65????while(p)
?66????{
?67????????listnode<T>?*q=p->next;
?68????????delete?p;
?69????????p=q;
?70????????size--;
?71????}

?72????header=NULL;
?73}

?74
?75template<typename?T>
?76void?linkedlist<T>::addNode(listnode<T>?*node,?int?pos)
?77{
?78????listnode<T>?*tmp=header;
?79????if(pos==0?||?tmp==NULL)
?80????{
?81????????node->next=tmp;
?82????????header=node;
?83????????size++;
?84????}

?85????else
?86????{
?87????????int?cnt=0;
?88????????while(tmp->next?&&?cnt<pos)
?89????????{
?90????????????tmp?=?tmp->next;
?91????????????cnt++;
?92????????}

?93????????node->next=tmp->next;
?94????????tmp->next=node;
?95????????size++;
?96????}

?97}

?98
?99template<typename?T>
100void?linkedlist<T>::deleteNode(int?pos)
101{
102????listnode<T>?*p,*q;
103????p=header;
104????if(p==NULL)
105????{
106????????return;
107????}

108????else?if(pos==0)
109????{
110????????q=p->next;
111????????delete?p;
112????????header=q;
113????????size--;
114????}

115????else
116????{
117????????int?cnt=0;
118????????while(cnt<pos-1?&&?p->next)
119????????{
120????????????p=p->next;
121????????????cnt++;
122????????}

123????????q=p->next;
124????????p->next=q->next;
125????????delete?q;
126????????size--;
127????}

128}

129
130template<typename?T>
131void?linkedlist<T>::printlist()
132{
133????listnode<T>?*tmp=header;
134????if(tmp==NULL)
135????{
136????????cout<<"empty?linked?list"<<endl;
137????????return;
138????}

139????else
140????{
141????????while(tmp)
142????????{
143????????????cout<<tmp->data<<"?";
144????????????tmp=tmp->next;
145????????}

146????????cout<<endl;
147????}

148}

149
150int?main()
151{
152????linkedlist<int>?ilist;
153
154????for(int?i=0;i<8;i++)
155????{
156????????listnode<int>?*node=new?listnode<int>(i);
157????????ilist.addNode(node,0);
158????}

159????ilist.printlist();
160
161????for(int?i=7;i>=0;i--)
162????{
163????????ilist.deleteNode(i);
164????????ilist.printlist();
165????}

166
167
168????return?0;
169}

170

?

看來(lái)現(xiàn)在對(duì)模板的支持比VC6要好多了。不過(guò)在使用模板時(shí)還是要小心。

?

在實(shí)現(xiàn)的時(shí)候,發(fā)現(xiàn)deleteNode的實(shí)現(xiàn)還是有問(wèn)題。需要進(jìn)行修改。

當(dāng)處理超出范圍的pos時(shí)有問(wèn)題。修改之后的

?

Code
?1void?linkedlist::deleteNode(int?pos)
?2{
?3????listnode?*p=header;
?4????listnode?*q;
?5????if?(p==NULL)
?6????{
?7????????return;
?8????}

?9????else?if(pos==0)
10????{
11????????size--;
12????????q=p->next;
13????????delete?p;
14????????header=q;
15????}

16????else
17????{
18????????int?cnt=0;
19??????while?(cnt<pos-1?&&?p->next->next)
20????????{
21????????????p=p->next;
22????????????cnt++;
23????????}

24????????size--;
25????????q=p->next;
26????????p->next=q->next;
27????????delete?q;
28????}

29}

30

?

模板可以使用typenameclass兩個(gè)關(guān)鍵詞,我比較習(xí)慣使用typename。大家可以想一想為什么會(huì)使用class作為模板關(guān)鍵字?是否有什么隱含意義?

Code
??1#include?<iostream>
??2using?namespace?std;
??3
??4template?<typename?T>
??5class?linkedlist
??6{
??7public:
??8????linkedlist();
??9????~linkedlist();
?10
?11????void?addNode(T?data,?int?pos);
?12????void?deleteNode(int?pos);
?13????void?printlist();
?14private:
?15????void?Destroy();
?16private:
?17????template<typename?T>
?18????class?listnode
?19????{
?20????public:
?21????????T?data;
?22????????listnode<T>?*next;
?23
?24????????listnode(T?d,?listnode<T>?*n=NULL):data(d),next(n)
?25????????{}
?26????}
;
?27????listnode<T>?*header;
?28????int?size;
?29}
;
?30
?31template<typename?T>
?32linkedlist<T>::linkedlist()
?33{
?34????header=NULL;
?35????size=0;
?36}

?37
?38template<typename?T>
?39linkedlist<T>::~linkedlist()
?40{
?41????if(header)
?42????????Destroy();
?43????header=NULL;
?44????size=0;
?45}

?46
?47template<typename?T>
?48void?linkedlist<T>::Destroy()
?49{
?50????listnode<T>?*p=header;
?51????while(p)
?52????{
?53????????listnode<T>?*q=p->next;
?54????????delete?p;
?55????????p=q;
?56????????size--;
?57????}

?58????header=NULL;
?59}

?60
?61template<typename?T>
?62void?linkedlist<T>::addNode(T?data,?int?pos)
?63{
?64????listnode<T>?*node=new?listnode<T>(data);
?65????listnode<T>?*tmp=header;
?66????if(tmp==NULL?||?pos==0)
?67????{
?68????????node->next=tmp;
?69????????header=node;
?70????????size++;
?71????}

?72????else
?73????{
?74????????int?cnt=0;
?75????????while(tmp->next?&&?cnt<pos)
?76????????{
?77????????????tmp=tmp->next;
?78????????????cnt++;
?79????????}

?80????????node->next=tmp->next;
?81????????tmp->next=node;
?82????????size++;
?83????}

?84}

?85
?86template<typename?T>
?87void?linkedlist<T>::deleteNode(int?pos)
?88{
?89????listnode<T>?*p,*q;
?90????p=header;
?91????if(p==NULL)
?92????????return;
?93????else?if(pos==0)
?94????{
?95????????q=p->next;
?96????????delete?p;
?97????????header=q;
?98????}

?99????else
100????{
101????????int?cnt=0;
102????????while(cnt<pos-1?&&?p->next->next)
103????????{
104????????????p=p->next;
105????????????cnt++;
106????????}

107????????q=p->next;
108????????p->next=q->next;
109????????delete?q;
110????}

111????size--;
112}

113
114template<typename?T>
115void?linkedlist<T>::printlist()
116{
117????listnode<T>?*tmp=header;
118????if(tmp==NULL)
119????????cout<<"empty?linked?list."<<endl;
120????else
121????{
122????????while(tmp)
123????????{
124????????????cout<<tmp->data<<"?";
125????????????tmp=tmp->next;
126????????}

127????????cout<<endl;
128????}

129}

130
131int?main()
132{
133????linkedlist<int>?ilist;
134????
135????for(int?i=0;i<8;i++)
136????{
137????????ilist.addNode(i,0);
138????}

139
140????ilist.printlist();
141
142????return?0;
143}

144

?

關(guān)于嵌套類(lèi)的模板函數(shù),也一樣實(shí)現(xiàn)了,很奇怪的沒(méi)有出現(xiàn)什么問(wèn)題。

當(dāng)然,有一些函數(shù)重載沒(méi)有實(shí)現(xiàn)。不知道那些有沒(méi)有什么問(wèn)題,不過(guò)從目前的實(shí)現(xiàn)說(shuō)明VS對(duì)于模板的支持越來(lái)越好了。

到此為止,所有的具體實(shí)現(xiàn)都已經(jīng)結(jié)束。下面是侯捷老師自己分析的STL源碼,從其網(wǎng)站上可以得到,我這里將其下載了下來(lái)。作為單獨(dú)一篇發(fā)表。

轉(zhuǎn)載于:https://www.cnblogs.com/cnyao/archive/2009/10/22/linkedlist13.html

總結(jié)

以上是生活随笔為你收集整理的穷究链表(十三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。