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

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

    You use a pointer, otherwise your code:

    class Node
    {
       //etc
       Node m_next; //non-pointer
    };
    

    …would not compile, as the compiler cannot compute the size of Node. This is because it depends on itself — which means the compiler cannot decide how much memory it would consume.

提交回复
热议问题