Why do linked lists use pointers instead of storing nodes inside of nodes

后端 未结 11 2183
春和景丽
春和景丽 2020-12-07 13:53

I\'ve worked with linked lists before extensively in Java, but I\'m very new to C++. I was using this node class that was given to me in a project just fine

         


        
11条回答
  •  死守一世寂寞
    2020-12-07 14:12

    In Java

    Node m_node
    

    stores a pointer to another node. You don't have a choice about it. In C++

    Node *m_node
    

    means the same thing. The difference is that in C++ you can actually store the object as opposed to a pointer to it. That's why you have to say you want a pointer. In C++:

    Node m_node
    

    means store the node right here (and that clearly can't work for a list - you end up with a recursively defined structure).

提交回复
热议问题