Reputation: 145
The insert function on my single linked list doesn't work. I've narrowed down the problem to the while loop statement. Since the statement has an AND operator, the entire statement either evaluates to true
or false
.
while(value > nodePointer->value && nodePointer != nullptr)
this is what I wrote, and it doesnt work
while(nodePointer != nullptr && value > nodePointer->value)
I tried this and it DOES work, but I dont know why
if its all going to evaluate to true or false, why does the order matter?
class NumberList
{
private:
struct listNode
{
double value;
struct listNode *next;
};
listNode *head;
public:
NumberList()
{
head = nullptr;
}
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void displayNode();
};
void NumberList::insertNode(double value)
{
listNode* newNode;
listNode* nodePointer;
listNode* previousPointer;
newNode = new listNode;
newNode->value = value;
newNode->next = nullptr;
if (!head) //if nothing in the list
{
head = newNode;
newNode->next = nullptr;
cout << "check1" << endl;
}
else
{
nodePointer = head; //initial placement and reset
previousPointer = nullptr;
cout << "check2" << endl;
while (value > nodePointer->value && nodePointer != nullptr)
{
previousPointer = nodePointer;
nodePointer = nodePointer->next;
cout << "check3" << endl;
}
if (previousPointer == nullptr) //if the value needs to be inserted at the front
{
head = newNode; //head points to newly instered node
newNode->next = nodePointer;//newly insterted node points to the next node
cout << "check4" << endl;
}
else//everywhere else
{
previousPointer->next = newNode;
newNode->next = nodePointer;
cout << "check5" << endl;
}
}
}
Upvotes: 0
Views: 60
Reputation: 118445
while (value > nodePointer->value && nodePointer != nullptr)
When nodePointer
reaches the end of the road and becomes NULL
, the above condition will check nodePointer->value
first, before checking whether nodePointer
is NULL
. This is not going to end well.
You need to check for the NULL
pointer as the first order of business here, and before doing anything else with it, like wondering what's the value
of the object it's pointing to. The &&
operator evaluates the left-hand side first, and only evaluates the right hand side if it needs to. If the left-hand side is false
, the right-hand side is not evaluated. That's how it works.
Upvotes: 2
Reputation: 87997
if its all going to evaluate to true or false,`
That's your mistake, it's not all going to be evaluated. In the expression A && B
if A
evaluates to false then B
will not be evaluated at all. This is sometimes called short circuit evaluation.
Now look at your two versions when nodePointer
is null.
while (value > nodePointer->value && nodePointer != nullptr)
In this case nodePointer->value
causes an error, you can't dereference a null pointer. Now the other version
while (nodePointer != nullptr && value > nodePointer->value)
In this code nodePointer != nullptr
is false so value > nodePointer->value
is not evaluated, so it does not cause an error. That's the difference.
A similar rule applies to A || B
. In this expression if A
evaluates to true B
will not be evaluated at all.
Upvotes: 1