穷究链表(十二)
在实现模板之前,我们来实现一下嵌套类。
在实现模板之前,我们来实现一下嵌套类。
1
#include <iostream>2
using namespace std;3

4
class linkedlist5


{6
public:7
class listnode8

{9
public:10
int data;11
listnode *next;12

13
public:14
listnode(int d=0, listnode* n=NULL):data(d),next(n) 15

{16
}17
public:18

int getdata()
{return data;}19
};20

21
public:22
linkedlist();23
~linkedlist();24
linkedlist(const linkedlist& list2);25
linkedlist& operator=(const linkedlist& list);26

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

40
linkedlist::linkedlist()41


{42
header=NULL;43
size=0;44
}45

46
linkedlist::~linkedlist()47


{48
if (header)49

{50
Destroy();51
}52
header=NULL;53
size=0;54
}55

56
void linkedlist::Destroy()57


{58
listnode *p = header;59
while (p)60

{61
listnode *q = p->next;62
delete p;63
p=q;64
}65
header=NULL;66
}67

68
linkedlist::linkedlist(const linkedlist& list2)69


{70
listnode *tmp=list2.header;71
listnode *prev;72
while (tmp)73

{74
listnode *node=new listnode(tmp->data);75
if (size==0)76

{77
header=node;78
prev=node;79
}80
else81

{82
prev->next=node;83
prev=node;84
}85
size++;86

87
tmp=tmp->next;88
}89
}90

91
linkedlist& linkedlist::operator=(const linkedlist& mylist)92


{93
if (mylist.header==this->header) //?????????94

{95
return *this;96
}97
else98

{99
listnode *tmp=mylist.header;100
listnode *prev;101
while (tmp)102

{103
listnode *node= new listnode(tmp->data);104
if (size==0)105

{106
header=node;107
prev=node;108
}109
else110

{111
prev->next=node;112
prev=node;113
}114
size++;115

116
tmp=tmp->next;117
}118
}119
return *this;120
}121

122
void linkedlist::addNode(listnode &node, int pos)123


{124
listnode *tmp=header;125
int cnt=0;126
if (tmp==NULL || pos==0)127

{128
size++;129
node.next=tmp;130
header=&node;131
return;132
}133
while(tmp->next && pos<cnt)134

{135
tmp=tmp->next;136
cnt++;137
}138
size++;139
node.next=tmp->next;140
tmp->next=&node;141
}142

143
void linkedlist::deleteNode(int pos)144


{145
listnode *p=header;146
listnode *q;147
if (p==NULL)148

{149
return;150
}151
else if(pos==0)152

{153
size--;154
q=p->next;155
delete p;156
header=q;157
}158
else159

{160
int cnt=0;161
while (cnt<pos && p->next)162

{163
p=p->next;164
cnt++;165
}166
size--;167
q=p->next;168
p->next=q->next;169
delete q;170
}171
}172

173
void linkedlist::printlist()174


{175
if (size==0)176

{177
cout<<"empty linked list!"<<endl;178
}179
else180

{181
listnode *tmp = header;182
while(tmp)183

{184
cout<<tmp->data<<" ";185
tmp=tmp->next;186
}187
cout<<endl;188
}189
}190

191
void linkedlist::reverselist()192


{193
listnode *p,*q,*r;194
p=header;195
if (p==NULL || p->next==NULL)196

{197
return;198
}199
q=p->next;200
p->next=NULL;201
while (q)202

{203
r=q->next;204
q->next=p;205
p=q;206
q=r;207
}208
header=p;209
}210

211
void linkedlist::mergelist(linkedlist& list)212


{213
listnode *header2=list.header;214
listnode *tmp = header;215
if(tmp)216

{217
while (tmp->next)218

{219
tmp=tmp->next;220
}221
tmp->next=header2;222
}223
else224

{225
header=header2;226
}227
list.header=NULL;228
}229

230
linkedlist::listnode* linkedlist::findNode(int pos)231


{232
listnode *tmp=header;233
int cnt=0;234
while (tmp && cnt<pos)235

{236
tmp = tmp->next;237
cnt++;238
}239
return tmp;240
}241

242
int main()243


{244
linkedlist llist;245
for (int i=0;i<8;i++)246

{247
//listnode node(i);248
linkedlist::listnode *node = new linkedlist::listnode(i);249
llist.addNode(*node,0);250
}251

252
llist.printlist();253

254
linkedlist::listnode *n=llist.findNode(8);255
if (n==NULL)256

{257
cout<<"cannot find such node"<<endl;258
}259
else260

{261
cout<<n->getdata()<<endl;262
}263

264
linkedlist llist2;265
for (int i=50;i<56;i++)266

{267
linkedlist::listnode *node = new linkedlist::listnode(i);268
llist2.addNode(*node,0);269
}270

271
llist2.printlist();272

273
llist.mergelist(llist2);274
llist.printlist();275

276
return 0;277
}278

可能还需要修改,因为这不是我想要的那样。(不过后面发现,使用了嵌套类之后还是需要声明友元,这样另外搞嵌套类就似乎多此一举了)
所以,到现在为止,我遗留了两个明显的问题,和一个其他问题。明显的问题就是,在赋值函数的判断中,到底如何判断,还没有确定。还有就是,嵌套类的实现,没有达到符合的标准。其他问题就是,测试没有完全,怎么确定代码的正确性。
对于private成员,只有该类或者友元才可以访问,除非外围类(outer class)被声明为嵌套类(inner class)的友元,否则它没有权利访问嵌套类的私有成员。
http://blog.csdn.net/wuliming_sc/archive/2009/01/21/3845428.aspx
在实现的时候,发现
1
int linkedlist::findNode(int pos)
2

{
3
listnode *tmp=header;
4
int cnt=0;
5
while (tmp->next && cnt<pos)
6
{
7
tmp = tmp->next;
8
cnt++;
9
}
10
return tmp->data;
11
}
12
int linkedlist::findNode(int pos)2


{3
listnode *tmp=header;4
int cnt=0;5
while (tmp->next && cnt<pos)6

{7
tmp = tmp->next;8
cnt++;9
}10
return tmp->data;11
}12

修改可能还有些问题,对于找不到节点的情况,不知道怎么处理比较好。
此时引入异常(我就知道我会碰到这家伙,不过不清楚是什么时候)。
经过修改后的代码暂时如下:
1
#include <iostream>2
using namespace std;3

4
class linkedlist5


{6
private:7
class listnode8

{9
public:10
int data;11
listnode *next;12

13
public:14
listnode(int d=0, listnode* n=NULL):data(d),next(n) 15

{16
}17
};18

19
public:20
linkedlist();21
~linkedlist();22
linkedlist(const linkedlist& list2);23
linkedlist& operator=(const linkedlist& list);24

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

38
linkedlist::linkedlist()39


{40
header=NULL;41
size=0;42
}43

44
linkedlist::~linkedlist()45


{46
if (header)47

{48
Destroy();49
}50
header=NULL;51
size=0;52
}53

54
void linkedlist::Destroy()55


{56
listnode *p = header;57
while (p)58

{59
listnode *q = p->next;60
delete p;61
p=q;62
}63
header=NULL;64
}65

66
linkedlist::linkedlist(const linkedlist& list2)67


{68
listnode *tmp=list2.header;69
listnode *prev;70
while (tmp)71

{72
listnode *node=new listnode(tmp->data);73
if (size==0)74

{75
header=node;76
prev=node;77
}78
else79

{80
prev->next=node;81
prev=node;82
}83
size++;84

85
tmp=tmp->next;86
}87
}88

89
linkedlist& linkedlist::operator=(const linkedlist& mylist)90


{91
if (mylist.header==this->header) //?????????92

{93
return *this;94
}95
else96

{97
listnode *tmp=mylist.header;98
listnode *prev;99
while (tmp)100

{101
listnode *node= new listnode(tmp->data);102
if (size==0)103

{104
header=node;105
prev=node;106
}107
else108

{109
prev->next=node;110
prev=node;111
}112
size++;113

114
tmp=tmp->next;115
}116
}117
return *this;118
}119

120
void linkedlist::addNode(int nodeval, int pos)121


{122
listnode *tmp=header;123
int cnt=0;124
listnode *node = new listnode(nodeval);125
if (tmp==NULL || pos==0)126

{127
size++;128
node->next=tmp;129
header=node;130
return;131
}132
while(tmp->next && pos<cnt)133

{134
tmp=tmp->next;135
cnt++;136
}137
size++;138
node->next=tmp->next;139
tmp->next=node;140
}141

142
void linkedlist::deleteNode(int pos)143


{144
listnode *p=header;145
listnode *q;146
if (p==NULL)147

{148
return;149
}150
else if(pos==0)151

{152
size--;153
q=p->next;154
delete p;155
header=q;156
}157
else158

{159
int cnt=0;160
while (cnt<pos && p->next)161

{162
p=p->next;163
cnt++;164
}165
size--;166
q=p->next;167
p->next=q->next;168
delete q;169
}170
}171

172
void linkedlist::printlist()173


{174
if (size==0)175

{176
cout<<"empty linked list!"<<endl;177
}178
else179

{180
listnode *tmp = header;181
while(tmp)182

{183
cout<<tmp->data<<" ";184
tmp=tmp->next;185
}186
cout<<endl;187
}188
}189

190
void linkedlist::reverselist()191


{192
listnode *p,*q,*r;193
p=header;194
if (p==NULL || p->next==NULL)195

{196
return;197
}198
q=p->next;199
p->next=NULL;200
while (q)201

{202
r=q->next;203
q->next=p;204
p=q;205
q=r;206
}207
header=p;208
}209

210
void linkedlist::mergelist(linkedlist& list)211


{212
listnode *header2=list.header;213
listnode *tmp = header;214
if(tmp)215

{216
while (tmp->next)217

{218
tmp=tmp->next;219
}220
tmp->next=header2;221
}222
else223

{224
header=header2;225
}226
list.header=NULL;227
}228

229
int linkedlist::findNode(int pos)230


{231
listnode *tmp=header;232
int cnt=0;233
while (tmp && cnt<pos)234

{235
tmp = tmp->next;236
cnt++;237
}238
if(tmp!=NULL)239
return tmp->data;240
else241
throw "there are no such node";242
}243

244
int main()245


{246
linkedlist llist;247
for (int i=0;i<8;i++)248

{249
llist.addNode(i,0);250
}251

252
llist.printlist();253

254
try255

{256
int n=llist.findNode(8);257
cout<<n<<endl;258
}259
catch(char *)260

{261
cout<<"exception catched"<<endl;262
}263
//if (n==NULL)264
//{265
// cout<<"cannot find such node"<<endl;266
//}267
//else268
//{269
// cout<<n->getdata()<<endl;270
//}271

272
linkedlist llist2;273
for (int i=50;i<56;i++)274

{275
llist2.addNode(i,0);276
}277

278
llist2.printlist();279

280
llist.mergelist(llist2);281
llist.printlist();282

283
return 0;284
}285

可以看到上面某些函数代码中加入了异常处理。
原先是采用catch(...)的,后来换成catch(char *),不过似乎感觉还是不太好。不过如果要自己定义一个exception类又太麻烦了。不清楚是否有什么比较好的方法。
从异常处理来说,JAVA做得比C++要好些,至少很多exception类比较齐全,同时报错时,异常栈中的信息比较全。
浙公网安备 33010602011771号