Chain Chain::insert(int x) {
ChainNode * current = first;
ChainNode * ptr = new ChainNode;
ptr->data = x;
ptr->link = NULL;
if(first != NULL) {
while(current->link != NULL) {
current = current->link;
}
current->link = ptr;
return * this;
}
else {
first = ptr;
return * this;
}
}
如果不添加“&”,上述代码无效(参考)在回报声明中,即它给出了错误的结果。这里,Chain是用于实现链表的类,ChainNode是另一个对应于链表节点的类。