[英]insert function on a linked list doesn't work
The insert function on my single linked list doesn't work.在我的单链表上插入 function 不起作用。 I've narrowed down the problem to the while loop statement.
我已将问题缩小到 while 循环语句。 Since the statement has an AND operator, the entire statement either evaluates to
true
or false
.由于该语句具有 AND 运算符,因此整个语句的计算结果为
true
或false
。 while(value > nodePointer->value && nodePointer != nullptr)
this is what I wrote, and it doesnt work while(value > nodePointer->value && nodePointer != nullptr)
这是我写的,它不起作用
while(nodePointer != nullptr && value > nodePointer->value)
I tried this and it DOES work, but I dont know why while(nodePointer != nullptr && value > nodePointer->value)
我试过了,它确实有效,但我不知道为什么
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;
}
}
}
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
.当
nodePointer
到达路的尽头变成NULL
时,上述条件会先检查nodePointer->value
,然后再检查nodePointer
是否为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.您需要检查
NULL
指针作为这里的首要业务,然后再对它进行任何其他操作,例如想知道它指向的 object 的value
是多少。 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.如果左侧为
false
,则不评估右侧。 That's how it works.这就是它的工作原理。
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.在表达式
A && B
中,如果A
计算结果为 false,则根本不会计算B
This is sometimes called short circuit evaluation .这有时称为短路评估。
Now look at your two versions when nodePointer
is null.现在看看你的两个版本,
nodePointer
是 null。
while (value > nodePointer->value && nodePointer != nullptr)
In this case nodePointer->value
causes an error, you can't dereference a null pointer.在这种情况下
nodePointer->value
会导致错误,您不能取消引用 null 指针。 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.在此代码
nodePointer != nullptr
为 false,因此value > nodePointer->value
不会被评估,因此不会导致错误。 That's the difference.这就是区别。
A similar rule applies to A || B
类似的规则适用于
A || B
A || B
. A || B
。 In this expression if A
evaluates to true B
will not be evaluated at all.在这个表达式中,如果
A
计算结果为真,则B
根本不会被计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.