linked list?

I am trying to fully understand linked lists, and there is one part that is really bugging me. Hoping someone could clarify it for me.

If I create a node pointer in an empty list, and set the node pointer = to the head pointer. That new node pointer will just point to null too correct?

The next question I am having a bit of difficulty explaining so might not make a lot of sense.
If I create a node pointer in a list with elements, and set it to the last node element in the list, if i was to do NodePtr-> next. Would that give me back the nullptr that is stored in the node (is the node pointer actually pointing to that element node still and just retrieving the next information from that element node) or is the node pointer now itself just pointing to null, and its that nullptr it returns?

It might help if i was to give an example. If i did a while loop like such

while (NodePtr != nullptr)
head = head->next;

Would the nodePtr be pointing to the last element after this loop, or does it pass the last element, and go to the nullptr that the last elements next was pointing too?
Pointers are variables that contain addresses.
Addresses are basically just numbers.
So if you assign the value of one node pointer to another then the other will contain the same number ("address") that the first contains.

A pointer never "points to" null. As one of its possible values, it can be null, which is a special value that means the pointer doesn't "point to" anything.

I can't understand your other question. Your example doesn't make sense. NodePtr is never modified.
If I create a node pointer in an empty list, and set the node pointer = to the head pointer. That new node pointer will just point to null too correct?

yes, assuming that head is null, then anything = head will also be null.

while (NodePtr != nullptr)
head = head->next;

^^^ this is infinite loop.

maybe you meant
while (head != nullptr)
head = head->next;

this is just a painfully slow way to set head to nullptr. it iterates the whole list and then sets head to nullptr.

pointers are just integers that are used in a special way.
int x = 3;
int y = x;
x = 4; //what is y? it is still 3!

nodeptr = head;
head = nullptr;
nodeptr is still whatever head was... they do not interact with each other, its not a reference.

however the DATA at the pointer's location is shared:
head->data = 3;
nodeptr = head;
nodeptr->data = 42;
//head->data is 42, not 3.

finally, to get the tail, you need special case logic..

if there are 0 or 1 items, head == tail //for 0 both are null, for 1 item, they share it
else if there are 2 items, tail = head -> next
else now you can loop looking ahead.
tmp = head;
while(tmp->next->next !=nullptr)
{tmp = tmp->next}
tail = tmp->next;

say you have 2 items 1->2->N
1 is tmp is head... 1.next is 2, 2.next is null, the loop doe not even execute (1.next.next is null)
tail = 2 (tmp which is head which is 1 .next is 2)

for 3 items, the loop executes once, and so on..

if you want to use the tail, keep it up to date everywhere instead of going hunting for it.
Last edited on
I can't understand your other question. Your example doesn't make sense. NodePtr is never modified.


Yes Ive noticed now that you and jonnin pointed it out, but I made a mistake with the loop. It should have been

while (nodePtr->next != nullptr)
{
nodePtr = nodePtr->next;
}

I was doing it this way, so i could find the last element in the list, and then "attach" the last elements next to the new node.

Thank you for the answers I believe I am understanding it better now.
Topic archived. No new replies allowed.