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

后端 未结 11 2182
春和景丽
春和景丽 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:04

    C++ is not Java. When you write

    Node m_next;
    

    in Java, that is the same as writing

    Node* m_next;
    

    in C++. In Java, the pointer is implicit, in C++ it is explicit. If you write

    Node m_next;
    

    in C++, you put an instance of Node right there inside the object that you are defining. It is always there and cannot be omitted, it cannot be allocated with new and it cannot be removed. This effect is impossible to achieve in Java, and it is totally different from what Java does with the same syntax.

提交回复
热议问题