copo
copo

Reputation: 41

Why the first node of a linked list is declared as a pointer?

Now I know that why pointers are used in defining linked lists. Simply because structure cannot have a recursive definition and if there would have been no pointers, the compiler won't be able to calculate the size of the node structure.

struct list{
        int data;
        struct list* next;    // this is fine
};

But confusion creeps up when I declare the first node of the linked list as:

struct list* head;

Why this has to be a pointer? Can't it be simply declared as

struct list head;

and the address of this used for further uses? Please clarify my doubt.

Upvotes: 1

Views: 2586

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

There's no definitive answer to this question. You can do it either way. The answer to this question depends on how you want to organize your linked list and how you want to represent an empty list.

You have two choices:

  1. A list without a "dummy" head element. In this case the empty list is represented by null in head pointer

    struct list* head = NULL;
    

    So this is the answer to your question: we declare it as a pointer to be able to represent an empty list by setting head pointer to null.

  2. A list with a "dummy" head element. In this case the first element of the list is not used to store actual user data: it simply serves as a starting "dummy" element of the list. It is declared as

    struct list head = { 0 };
    

    The above represents an empty list, since head.next is null and head object itself "does not count".

    I.e. you can declare it that way, if you so desire. Just keep in mind that head is not really a list element. The actual elements begin after head.

And, as always, keep in mind that when you use non-dynamically-allocated objects, the lifetime of those objects is governed by scoping rules. If you want to override these rules and control the objects' lifetimes manually, then you have no other choice but to allocate them dynamically and, therefore, use pointers.

Upvotes: 3

Rahul
Rahul

Reputation: 53

In simple terms, if your head is the start node of the linked list, then it will only contain the address of the first node from where linked list will begin. This is done to avoid confusion for a general programmer. Since the head will contain only address, hence, it is declared as a pointer. But the way you want to declare is also fine, just code accordingly. Tip: If you later on want to make some changes in your linked list, like deletion or insertion operations at the beginning of the linked list, you will face problems as you will require another pointer variable. So its better to declare the first node as pointer.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

You can declare a list such a way

struct list head = {};

But there will be some difficulties in the realization of functions that access the list. They have to take into account that the first node is not used as other nodes of the list and data member of the first node data also is not used.

Usually the list is declared the following way

struct List
{
    // some other stuff as for example constructors and member functions
    struct node
    {
        int data;
        struct node* next;    // this is fine
    } head;
};

and

List list = {};

Or in C++ you could write simply

struct List
{
    // some other stuff as for example constructors and member functions
    struct node
    {
        int data;
        struct node* next;    // this is fine
    } head = nullptr;
};

List list;

Of course you could define the default constructor of the List yourself.

In this case for example to check whether the list is empty it is enough to define the following member function

struct List
{
    bool empty() const { return head == nullptr; }

    // some other stuff as for example constructors and member functions

    struct node
    {
        int data;
        struct node* next;    // this is fine
    } head;
};

Upvotes: 0

Related Questions