我目前正在处理链表,该列表在列表的前面存储偶数整数,在后面存储奇数整数。除了我的删除功能外,一切都工作得很好,用户输入是否要删除偶数或奇数,并将其用作从列表的前面或后面删除的提示。这是我的节点删除功能的代码:
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构造中,我有用于删除优先级的代码上方删除evens的代码,它只会删除evens,反之亦然。请帮忙,我无法弄清楚它为什么会这样做。
答案 0 :(得分:2)
更改
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)