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

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

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

    There is of course a trivial answer.

    If they didn't link one node to the next by a pointer, they wouldn't be linked lists.

    The existence of linked lists as a thing is because we want to be able to chain objects together. For example: we already have an object from somewhere. We now want to put that actual object (not a copy) at the end of a queue, for example. That is achieved by adding a link from the last element already on the queue to the entry we are adding. In machine terms, that's filling in a word with the address of the next element.

提交回复
热议问题