前面完成了三个比较完整的链表程序。下面就是使用模板类来实现这个链表程序了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#include <iostream>
2
using namespace std;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
template<typename T>
5
class linkedlist;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
template<typename T>
8
class listnode
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
friend class linkedlist<T>;
11
private:
12
T data;
13
listnode<T> *next;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
public:
16
listnode(T d,listnode<T> *n=NULL):data(d),next(n)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
}
19
20
T getdata()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
return data;
23
}
24
};
25![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
template<typename T>
27
class linkedlist
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
29
public:
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);
38
private:
39
void Destroy();
40
private:
41
listnode<T> *header;
42
int size;
43
};
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
template<typename T>
46
linkedlist<T>::linkedlist()
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
48
header=NULL;
49
size=0;
50
}
51![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
template<typename T>
53
linkedlist<T>::~linkedlist()
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
55
if(header)
56
Destroy();
57
header=NULL;
58
size=0;
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
template<typename T>
62
void linkedlist<T>::Destroy()
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
64
listnode<T>* p=header;
65
while(p)
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
67
listnode<T> *q=p->next;
68
delete p;
69
p=q;
70
size--;
71
}
72
header=NULL;
73
}
74![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
template<typename T>
76
void linkedlist<T>::addNode(listnode<T> *node, int pos)
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
78
listnode<T> *tmp=header;
79
if(pos==0 || tmp==NULL)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
node->next=tmp;
82
header=node;
83
size++;
84
}
85
else
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
int cnt=0;
88
while(tmp->next && cnt<pos)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
tmp = tmp->next;
91
cnt++;
92
}
93
node->next=tmp->next;
94
tmp->next=node;
95
size++;
96
}
97
}
98![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99
template<typename T>
100
void linkedlist<T>::deleteNode(int pos)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
102
listnode<T> *p,*q;
103
p=header;
104
if(p==NULL)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
return;
107
}
108
else if(pos==0)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
q=p->next;
111
delete p;
112
header=q;
113
size--;
114
}
115
else
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
int cnt=0;
118
while(cnt<pos-1 && p->next)
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
p=p->next;
121
cnt++;
122
}
123
q=p->next;
124
p->next=q->next;
125
delete q;
126
size--;
127
}
128
}
129![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
130
template<typename T>
131
void linkedlist<T>::printlist()
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
133
listnode<T> *tmp=header;
134
if(tmp==NULL)
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
cout<<"empty linked list"<<endl;
137
return;
138
}
139
else
140![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
141
while(tmp)
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
143
cout<<tmp->data<<" ";
144
tmp=tmp->next;
145
}
146
cout<<endl;
147
}
148
}
149![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
150
int main()
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
152
linkedlist<int> ilist;
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
for(int i=0;i<8;i++)
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
listnode<int> *node=new listnode<int>(i);
157
ilist.addNode(node,0);
158
}
159
ilist.printlist();
160![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
161
for(int i=7;i>=0;i--)
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
ilist.deleteNode(i);
164
ilist.printlist();
165
}
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
168
return 0;
169
}
170![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
看来现在对模板的支持比VC6要好多了。不过在使用模板时还是要小心。
在实现的时候,发现deleteNode的实现还是有问题。需要进行修改。
当处理超出范围的pos时有问题。修改之后的
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
void linkedlist::deleteNode(int pos)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
listnode *p=header;
4
listnode *q;
5
if (p==NULL)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
return;
8
}
9
else if(pos==0)
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
size--;
12
q=p->next;
13
delete p;
14
header=q;
15
}
16
else
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
int cnt=0;
19
while (cnt<pos-1 && p->next->next)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
p=p->next;
22
cnt++;
23
}
24
size--;
25
q=p->next;
26
p->next=q->next;
27
delete q;
28
}
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
模板可以使用typename和class两个关键词,我比较习惯使用typename。大家可以想一想为什么会使用class作为模板关键字?是否有什么隐含意义?
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#include <iostream>
2
using namespace std;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
template <typename T>
5
class linkedlist
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public:
8
linkedlist();
9
~linkedlist();
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
void addNode(T data, int pos);
12
void deleteNode(int pos);
13
void printlist();
14
private:
15
void Destroy();
16
private:
17
template<typename T>
18
class listnode
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
public:
21
T data;
22
listnode<T> *next;
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
listnode(T d, listnode<T> *n=NULL):data(d),next(n)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
26
};
27
listnode<T> *header;
28
int size;
29
};
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
template<typename T>
32
linkedlist<T>::linkedlist()
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
34
header=NULL;
35
size=0;
36
}
37![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
template<typename T>
39
linkedlist<T>::~linkedlist()
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
41
if(header)
42
Destroy();
43
header=NULL;
44
size=0;
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
template<typename T>
48
void linkedlist<T>::Destroy()
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
50
listnode<T> *p=header;
51
while(p)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
listnode<T> *q=p->next;
54
delete p;
55
p=q;
56
size--;
57
}
58
header=NULL;
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
template<typename T>
62
void linkedlist<T>::addNode(T data, int pos)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
64
listnode<T> *node=new listnode<T>(data);
65
listnode<T> *tmp=header;
66
if(tmp==NULL || pos==0)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
node->next=tmp;
69
header=node;
70
size++;
71
}
72
else
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
int cnt=0;
75
while(tmp->next && cnt<pos)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
tmp=tmp->next;
78
cnt++;
79
}
80
node->next=tmp->next;
81
tmp->next=node;
82
size++;
83
}
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
86
template<typename T>
87
void linkedlist<T>::deleteNode(int pos)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
89
listnode<T> *p,*q;
90
p=header;
91
if(p==NULL)
92
return;
93
else if(pos==0)
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
q=p->next;
96
delete p;
97
header=q;
98
}
99
else
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
int cnt=0;
102
while(cnt<pos-1 && p->next->next)
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
p=p->next;
105
cnt++;
106
}
107
q=p->next;
108
p->next=q->next;
109
delete q;
110
}
111
size--;
112
}
113![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
114
template<typename T>
115
void linkedlist<T>::printlist()
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
117
listnode<T> *tmp=header;
118
if(tmp==NULL)
119
cout<<"empty linked list."<<endl;
120
else
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
while(tmp)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
cout<<tmp->data<<" ";
125
tmp=tmp->next;
126
}
127
cout<<endl;
128
}
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
131
int main()
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
133
linkedlist<int> ilist;
134
135
for(int i=0;i<8;i++)
136![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
137
ilist.addNode(i,0);
138
}
139![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
140
ilist.printlist();
141![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
142
return 0;
143
}
144![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
关于嵌套类的模板函数,也一样实现了,很奇怪的没有出现什么问题。
当然,有一些函数重载没有实现。不知道那些有没有什么问题,不过从目前的实现说明VS对于模板的支持越来越好了。
到此为止,所有的具体实现都已经结束。下面是侯捷老师自己分析的STL源码,从其网站上可以得到,我这里将其下载了下来。作为单独一篇发表。