-1

假设我有一个链表节点类

class node {
    private: 
        node *next_node;
    public:
        node *next() const;
};

node *node::next() const {
    return next_node;
}

next() 是否返回节点 **next_node 或节点 *next_node。另外,在实现列表类函数(即插入、删除、查找)中,两者的意义是什么?

我认为它返回 **next_node 的原因是因为 next_node 已经是一个指针,并且在函数中将它作为指针返回会使它成为指向指针的指针。我读过其他问题,例如:链表头双指针传递双指针也适用于列表操作,所以我有点困惑。

4

2 回答 2

0

The node::next() function as implemented is returning a copy of the next_node member variable. Both next_node and this copy point to the same node instance but they are otherwise independent of each other.

Here is some rather bad ASCII art that tries to demonstrate their relationship

next_node ----> [node instance]
                ^
the copy ------/
于 2012-04-13T16:14:39.717 回答
0

正如声明所说,它返回一个指向下一个节点的指针,类型为node*

它用于在列表中前进;例如:

for (node * n = list.head(); n; n = n->next()) {
    // Process node n
}
于 2012-04-13T16:15:26.453 回答