我想知道之间的区别setcdr,setcar,cdr和car.我知道car是指节点的值,而cdr函数是指节点中的下一个指针,但我不明白这些差异.
该setcdr函数:
void setcdr( Node* p, Node* q ) {
assert (p != nullptr);
p->next = q;
}
Run Code Online (Sandbox Code Playgroud)
是void,那么如何设置链表呢?不应该退货Node吗?
//returns the data field of the Node
// if p is the nullptr, we cannot get its data ...exit abruptly
int car( Node* p ) {
assert (p != nullptr);
return( p->value );
}
// returns the next field of the Node
// if p is the nullptr, we cannot get its next.... exit abruptly
Node* cdr( Node* p ) {
assert (p != nullptr);
return( p->next );
}
void setcar( Node* p, int x ) {
assert (p != nullptr);
p->value = x;
}
Run Code Online (Sandbox Code Playgroud)