在实现模板之前,我们来实现一下嵌套类。
![](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
class linkedlist
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public:
7
class listnode
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
public:
10
int data;
11
listnode *next;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public:
14
listnode(int d=0, listnode* n=NULL):data(d),next(n)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
}
17
public:
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int getdata()
{return data;}
19
};
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public:
22
linkedlist();
23
~linkedlist();
24
linkedlist(const linkedlist& list2);
25
linkedlist& operator=(const linkedlist& list);
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
void addNode(listnode& node, int pos);
28
void deleteNode(int pos);
29
void reverselist();
30
void printlist();
31
void mergelist(linkedlist& list);
32
listnode* findNode(int pos);
33
private:
34
void Destroy();
35
private:
36
listnode *header;
37
int size;
38
};
39![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
linkedlist::linkedlist()
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
42
header=NULL;
43
size=0;
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
linkedlist::~linkedlist()
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
48
if (header)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
Destroy();
51
}
52
header=NULL;
53
size=0;
54
}
55![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
void linkedlist::Destroy()
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
58
listnode *p = header;
59
while (p)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
listnode *q = p->next;
62
delete p;
63
p=q;
64
}
65
header=NULL;
66
}
67![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
linkedlist::linkedlist(const linkedlist& list2)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
70
listnode *tmp=list2.header;
71
listnode *prev;
72
while (tmp)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
listnode *node=new listnode(tmp->data);
75
if (size==0)
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
header=node;
78
prev=node;
79
}
80
else
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
82
prev->next=node;
83
prev=node;
84
}
85
size++;
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
tmp=tmp->next;
88
}
89
}
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
linkedlist& linkedlist::operator=(const linkedlist& mylist)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
93
if (mylist.header==this->header) //?????????
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
95
return *this;
96
}
97
else
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
listnode *tmp=mylist.header;
100
listnode *prev;
101
while (tmp)
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
listnode *node= new listnode(tmp->data);
104
if (size==0)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
header=node;
107
prev=node;
108
}
109
else
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
111
prev->next=node;
112
prev=node;
113
}
114
size++;
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
tmp=tmp->next;
117
}
118
}
119
return *this;
120
}
121![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
122
void linkedlist::addNode(listnode &node, int pos)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
124
listnode *tmp=header;
125
int cnt=0;
126
if (tmp==NULL || pos==0)
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
size++;
129
node.next=tmp;
130
header=&node;
131
return;
132
}
133
while(tmp->next && pos<cnt)
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
tmp=tmp->next;
136
cnt++;
137
}
138
size++;
139
node.next=tmp->next;
140
tmp->next=&node;
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
143
void linkedlist::deleteNode(int pos)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
145
listnode *p=header;
146
listnode *q;
147
if (p==NULL)
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
149
return;
150
}
151
else if(pos==0)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
size--;
154
q=p->next;
155
delete p;
156
header=q;
157
}
158
else
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
160
int cnt=0;
161
while (cnt<pos && p->next)
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
p=p->next;
164
cnt++;
165
}
166
size--;
167
q=p->next;
168
p->next=q->next;
169
delete q;
170
}
171
}
172![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
173
void linkedlist::printlist()
174![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
175
if (size==0)
176![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
177
cout<<"empty linked list!"<<endl;
178
}
179
else
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
181
listnode *tmp = header;
182
while(tmp)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
cout<<tmp->data<<" ";
185
tmp=tmp->next;
186
}
187
cout<<endl;
188
}
189
}
190![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
191
void linkedlist::reverselist()
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
193
listnode *p,*q,*r;
194
p=header;
195
if (p==NULL || p->next==NULL)
196![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
197
return;
198
}
199
q=p->next;
200
p->next=NULL;
201
while (q)
202![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
203
r=q->next;
204
q->next=p;
205
p=q;
206
q=r;
207
}
208
header=p;
209
}
210![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
211
void linkedlist::mergelist(linkedlist& list)
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
213
listnode *header2=list.header;
214
listnode *tmp = header;
215
if(tmp)
216![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
217
while (tmp->next)
218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
219
tmp=tmp->next;
220
}
221
tmp->next=header2;
222
}
223
else
224![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
225
header=header2;
226
}
227
list.header=NULL;
228
}
229![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
230
linkedlist::listnode* linkedlist::findNode(int pos)
231![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
232
listnode *tmp=header;
233
int cnt=0;
234
while (tmp && cnt<pos)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
236
tmp = tmp->next;
237
cnt++;
238
}
239
return tmp;
240
}
241![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
242
int main()
243![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
244
linkedlist llist;
245
for (int i=0;i<8;i++)
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
247
//listnode node(i);
248
linkedlist::listnode *node = new linkedlist::listnode(i);
249
llist.addNode(*node,0);
250
}
251![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
252
llist.printlist();
253![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254
linkedlist::listnode *n=llist.findNode(8);
255
if (n==NULL)
256![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
257
cout<<"cannot find such node"<<endl;
258
}
259
else
260![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
261
cout<<n->getdata()<<endl;
262
}
263![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
264
linkedlist llist2;
265
for (int i=50;i<56;i++)
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
linkedlist::listnode *node = new linkedlist::listnode(i);
268
llist2.addNode(*node,0);
269
}
270![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
271
llist2.printlist();
272![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
273
llist.mergelist(llist2);
274
llist.printlist();
275![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
276
return 0;
277
}
278![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
可能还需要修改,因为这不是我想要的那样。(不过后面发现,使用了嵌套类之后还是需要声明友元,这样另外搞嵌套类就似乎多此一举了)
所以,到现在为止,我遗留了两个明显的问题,和一个其他问题。明显的问题就是,在赋值函数的判断中,到底如何判断,还没有确定。还有就是,嵌套类的实现,没有达到符合的标准。其他问题就是,测试没有完全,怎么确定代码的正确性。
对于private成员,只有该类或者友元才可以访问,除非外围类(outer class)被声明为嵌套类(inner class)的友元,否则它没有权利访问嵌套类的私有成员。
http://blog.csdn.net/wuliming_sc/archive/2009/01/21/3845428.aspx
在实现的时候,发现
1
int linkedlist::findNode(int pos)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
listnode *tmp=header;
4
int cnt=0;
5
while (tmp->next && cnt<pos)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
tmp = tmp->next;
8
cnt++;
9
}
10
return tmp->data;
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
修改可能还有些问题,对于找不到节点的情况,不知道怎么处理比较好。
此时引入异常(我就知道我会碰到这家伙,不过不清楚是什么时候)。
经过修改后的代码暂时如下:
![](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
class linkedlist
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
private:
7
class listnode
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
public:
10
int data;
11
listnode *next;
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
public:
14
listnode(int d=0, listnode* n=NULL):data(d),next(n)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
}
17
};
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
public:
20
linkedlist();
21
~linkedlist();
22
linkedlist(const linkedlist& list2);
23
linkedlist& operator=(const linkedlist& list);
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
void addNode(int nodeval, int pos);
26
void deleteNode(int pos);
27
void reverselist();
28
void printlist();
29
void mergelist(linkedlist& list);
30
int findNode(int pos);
31
private:
32
void Destroy();
33
private:
34
listnode *header;
35
int size;
36
};
37![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
linkedlist::linkedlist()
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
40
header=NULL;
41
size=0;
42
}
43![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
linkedlist::~linkedlist()
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
46
if (header)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
Destroy();
49
}
50
header=NULL;
51
size=0;
52
}
53![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
void linkedlist::Destroy()
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
56
listnode *p = header;
57
while (p)
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59
listnode *q = p->next;
60
delete p;
61
p=q;
62
}
63
header=NULL;
64
}
65![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
linkedlist::linkedlist(const linkedlist& list2)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
68
listnode *tmp=list2.header;
69
listnode *prev;
70
while (tmp)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
72
listnode *node=new listnode(tmp->data);
73
if (size==0)
74![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
75
header=node;
76
prev=node;
77
}
78
else
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
prev->next=node;
81
prev=node;
82
}
83
size++;
84![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
tmp=tmp->next;
86
}
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
linkedlist& linkedlist::operator=(const linkedlist& mylist)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
91
if (mylist.header==this->header) //?????????
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
93
return *this;
94
}
95
else
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
listnode *tmp=mylist.header;
98
listnode *prev;
99
while (tmp)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
listnode *node= new listnode(tmp->data);
102
if (size==0)
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
header=node;
105
prev=node;
106
}
107
else
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
109
prev->next=node;
110
prev=node;
111
}
112
size++;
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
tmp=tmp->next;
115
}
116
}
117
return *this;
118
}
119![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
120
void linkedlist::addNode(int nodeval, int pos)
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
122
listnode *tmp=header;
123
int cnt=0;
124
listnode *node = new listnode(nodeval);
125
if (tmp==NULL || pos==0)
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
127
size++;
128
node->next=tmp;
129
header=node;
130
return;
131
}
132
while(tmp->next && pos<cnt)
133![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
tmp=tmp->next;
135
cnt++;
136
}
137
size++;
138
node->next=tmp->next;
139
tmp->next=node;
140
}
141![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
142
void linkedlist::deleteNode(int pos)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
144
listnode *p=header;
145
listnode *q;
146
if (p==NULL)
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
return;
149
}
150
else if(pos==0)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
152
size--;
153
q=p->next;
154
delete p;
155
header=q;
156
}
157
else
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159
int cnt=0;
160
while (cnt<pos && p->next)
161![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
162
p=p->next;
163
cnt++;
164
}
165
size--;
166
q=p->next;
167
p->next=q->next;
168
delete q;
169
}
170
}
171![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
172
void linkedlist::printlist()
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
174
if (size==0)
175![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
176
cout<<"empty linked list!"<<endl;
177
}
178
else
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
listnode *tmp = header;
181
while(tmp)
182![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
183
cout<<tmp->data<<" ";
184
tmp=tmp->next;
185
}
186
cout<<endl;
187
}
188
}
189![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
190
void linkedlist::reverselist()
191![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
192
listnode *p,*q,*r;
193
p=header;
194
if (p==NULL || p->next==NULL)
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
return;
197
}
198
q=p->next;
199
p->next=NULL;
200
while (q)
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202
r=q->next;
203
q->next=p;
204
p=q;
205
q=r;
206
}
207
header=p;
208
}
209![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
210
void linkedlist::mergelist(linkedlist& list)
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
212
listnode *header2=list.header;
213
listnode *tmp = header;
214
if(tmp)
215![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
216
while (tmp->next)
217![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218
tmp=tmp->next;
219
}
220
tmp->next=header2;
221
}
222
else
223![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
224
header=header2;
225
}
226
list.header=NULL;
227
}
228![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
229
int linkedlist::findNode(int pos)
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
231
listnode *tmp=header;
232
int cnt=0;
233
while (tmp && cnt<pos)
234![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
235
tmp = tmp->next;
236
cnt++;
237
}
238
if(tmp!=NULL)
239
return tmp->data;
240
else
241
throw "there are no such node";
242
}
243![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
244
int main()
245![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
246
linkedlist llist;
247
for (int i=0;i<8;i++)
248![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
249
llist.addNode(i,0);
250
}
251![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
252
llist.printlist();
253![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254
try
255![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
256
int n=llist.findNode(8);
257
cout<<n<<endl;
258
}
259
catch(char *)
260![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
261
cout<<"exception catched"<<endl;
262
}
263
//if (n==NULL)
264
//{
265
// cout<<"cannot find such node"<<endl;
266
//}
267
//else
268
//{
269
// cout<<n->getdata()<<endl;
270
//}
271![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
272
linkedlist llist2;
273
for (int i=50;i<56;i++)
274![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
275
llist2.addNode(i,0);
276
}
277![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
278
llist2.printlist();
279![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
280
llist.mergelist(llist2);
281
llist.printlist();
282![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
283
return 0;
284
}
285![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
可以看到上面某些函数代码中加入了异常处理。
原先是采用catch(...)的,后来换成catch(char *),不过似乎感觉还是不太好。不过如果要自己定义一个exception类又太麻烦了。不清楚是否有什么比较好的方法。
从异常处理来说,JAVA做得比C++要好些,至少很多exception类比较齐全,同时报错时,异常栈中的信息比较全。