user3239138
user3239138

Reputation: 143

creating multiple nodes in a linked list beginner

typedef struct LinkedListItem
{
    int Number;
    LinkedListItem* Next;


    LinkedListItem(int number)
    {
        Number = number;
        Next = NULL;
    }
} LinkedListItem;

LinkedListItem *head = NULL;

head = new LinkedListItem(1);
head->Next = new LinkedListItem(2);  //confusing
head->Next->Next = new LinkedListItem(3);  //confusing

Hello. I am a beginner learning about linked list. This is a piece of code I found. I understand everything up until the lines I marked above. I understand that the head pointer is set to NULL first so then it can later point to a new LinkedListItem.

for

head->Next = new LinkedListItem(2)

Can someone explain what is happening here? I know that the arrow means to dereference. My guess is that dereferencing Next?

Upvotes: 0

Views: 1696

Answers (4)

max_hassen
max_hassen

Reputation: 397

A linked list is a sequence of elements connected to each other via pointers. In your example Next pointer points to a next element in list. Dereferencing a pointer means to refer to an object a pointer points to. An arrow means accessing fields of an object a pointer points to.

LinkedListItem *head = NULL;

In this line head pointer is set to NULL, as you correctly understood.

head = new LinkedListItem(1);

In this line head is set to point to a newly created LinkedListItem. head->Number is set to 1 and head->Next is set to NULL (see constructor).

head->Next = new LinkedListItem(2);  

In this line new LinkedListItem is created and Next pointer of a LinkedListItem created in a previous line is set to point at it.

head->Next->Next = new LinkedListItem(3);

Just like in a previous line a new element is created and Next pointer of previous element is set to point at it.

Upvotes: 0

Engin Kayraklioglu
Engin Kayraklioglu

Reputation: 595

When you say dereferencing it generally means *head in your piece of example. However if you write head->Next it is equivalent to (*head).Next so in that sense it is dereferencing.

As for your question, yes it is dereferencing head and adding new nodes at the end of the linked list. However be aware that this is not the neatest way to add elements to linked list. Even though it depends upon the type of application you are using linked list for, most generally you add elements directly to the head. Or keep a tail pointer and add to tail.

Upvotes: 0

Blaz Bratanic
Blaz Bratanic

Reputation: 2279

The

head->Next = new LinkedListItem(2)

statement is equivalent to

(*head).Next = new LinkedListItem(2)

so you dereference head and assign to its Next element (which is a pointer) a pointer to a new LinkedListItem.

Upvotes: 0

Ade Miller
Ade Miller

Reputation: 13723

Those lines add new elements to the list by creating a new item and recording it's address in the pointer *Next. So head->Next refers to the next element in the list. Once this is created then you can add head->Next->Next.

LinkedListItem *head = NULL;

head = NULL

head = new LinkedListItem(1);

head { Number = 1, Next = NULL }

head->Next = new LinkedListItem(2);

head { Number = 1, Next = { Number = 2, Next = NULL } }

head->Next->Next = new LinkedListItem(3);

head { Number = 1, Next = { Number = 2, Next = { Number = 3, Next = NULL } }

Upvotes: 1

Related Questions