confusedguy95
confusedguy95

Reputation: 9

What does Node * head outside of the struct do?

So I have the class LinkedList. Can someone explain why node * head is outside of the struct and node * next, prev are inside the struct? What is the purpose of this and how does it differ from the others? thanks

class LinkedList
{
public:
        LinkedList();
        ~LinkedList();

        void add(char ch);
        bool find(char ch);
        bool del(char ch);
        void display(
private:
        struct node
        {
                char data;
                node * next;
                node * prev;          
        };
        node *head;
};

Upvotes: 0

Views: 562

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

This

    struct node
    {
            char data;
            node * next;
            node * prev;          
    };

is an inner declaration of a structure type within a class.

This

node *head;

is a declaration of a data member of the type struct node * of the class LinkedList. To make it more clear rewrite the list definition the following way

struct node
{
        char data;
        node * next;
        node * prev;          
};

class LinkedList
{
public:
        LinkedList();
        ~LinkedList();

        void add(char ch);
        bool find(char ch);
        bool del(char ch);
        void display(
private:
        node *head;
};

Declaring the structure inside the class definition as a private declaration makes the structure type invisible for the users of the linked list.

Upvotes: 3

Related Questions