我目前正在开发一个链表,它在列表的前面存储偶数整数,在后面存储奇数整数。对我来说,一切都很好,除了我的删除功能,它接受用户输入,决定他们是想删除偶数还是奇数整数,并将其用作从列表前面或后面删除的提示。这是我的节点删除功能的代码:
void Staque::pop(char EvenOrOdd)
{
if (!empty())
{
if (EvenOrOdd == 'O' || 'o')
{
//Creating pointers to find end of Staque
Staque::NodePointer prevPtr = myTop, //Pointer to find penultimate Node and set "next" value to 0
currentPtr = myTop; //Pointer to find Node to delete
//Assigning pointers to corresponding nodes
while (prevPtr->next->next != 0)
{
prevPtr = prevPtr->next;
}
while (currentPtr->next != 0)
{
currentPtr = currentPtr->next;
}
//Deleting the last node in the Staque and setting "next" value of new end to 0
delete currentPtr;
prevPtr->next = 0;
}
else if (EvenOrOdd == 'e' || 'E')
{
Staque::NodePointer ptr = myTop;
myTop = myTop->next;
delete ptr;
}
}
else
{
cerr << "Stack is empty -- can't remove a value n";
}
问题是,它不需要输入来决定删除哪种元素。它只是删除我在节点删除函数的if构造中首先放入的内容。如果在If构造中,我在删除赔率的代码之上有删除偶数的代码,那么它只会删除偶数,反之亦然。请帮忙,我不明白它为什么这么做。
更改
if (EvenOrOdd == 'O' || 'o')
至
if (EvenOrOdd == 'O' || EvenOrOdd == 'o')
类似于另一个,即else if (EvenOrOdd == 'e' || 'E')
。
注意,对于
if (EvenOrOdd == 'O' || 'o')
这与相同
if (EvenOrOdd == 'O' || (bool)'o')
进一步到
if (EvenOrOdd == 'O' || true)
进一步到
if (true)