Search code examples
c++if-statementlinked-listnodes

Is this a way to check whether or not a LinkedList node is initialized?


I am trying to create an insert-at-end function for a linked list class. I was having some trouble, so I looked up some code and found the following:

if (!start) // empty list becomes the new node
{
    start = newNode;
    return;
}

This seems to be a check to make sure that if the user uses the function with an empty list, the newly created "ending" node, newNode, becomes the start node. But, what does if (!start) do? (start is the head node).


Solution

  • if (!start) // empty list becomes the new node
    {
        start = newNode;
        return;
    }
    

    ! negates whats in start. So if start is NULL then !NULL meaning true so it goes inside and make it as a start node. Similarly when start is not NULL then it doesn't go inside.