Search code examples
c++data-structureslinked-listcomputer-science

Why List class has to contain a Node struct as a private member C++


The implementation of Linked List in Mark Weiss's data structure book confuses me a little.

The List class contains a Node struct inside as follows.

...
class List {
    private:
        struct Node {
            ...
        };
    ... 

    public:
        ...

    private:
        int theSize;
        Node *head;
        Node *tail;
};

My question is that is having a Node struct inside the List class really necessary? I think as long as the List class contains pointer to the header and tail node is enough. What is the advantage of having a Node struct as a private member?

Thanks!


Solution

  • The reason that the Node structure would be declared as a private declaration within the List class is to keep it private to the implementation. This will ensure that the specific details of the implementation do not leak into the public interface. This is an effective way to abstract the interface from the internal implementation details, leaving those details free to change without impacting users of the List classes public interface.