jellyxbean
jellyxbean

Reputation: 83

C++ syntax questions (linked list)

I'm looking at some sample code that answers the question: remove all elements from a linked list of integers that have value val.

The first line of the code is if (!head) return NULL;. What does the (!head) mean? I'm assuming it means (head == NULL), but is this standard for anytime I'd like to say something like (head == NULL)?


while (h->next){
        if (h->next->val == val){
            h->next = h->next->next;

For this section of code, why is it okay to not include the != NULL part? (e.g. (h->next != NULL) ) Is that part implied in the statement without having to explicitly state it?

Upvotes: 0

Views: 54

Answers (1)

will ewing
will ewing

Reputation: 46

First off, ! is the boolean negation operation. It (!head) is basically head == 0 or head == false. For the seond part:

Zero is false, and NULL is (almost) always zero, so while(h->next) is basically equivalent while(h->next != NULL)

Upvotes: 3

Related Questions