0
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?

4 Answers 4

1

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 } }

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

1 Comment

You have a typo, head->Next is actually equivalent to (*head).Next
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.