Stack Pop无法使用LinkedLists处理自制的堆栈

时间:2018-09-26 02:28:38

标签: c++ stack singly-linked-list

我的堆栈的弹出功能不起作用。我包括了pop函数的代码。我知道我可以在C ++中使用堆栈库,但是我想自己动手以更好地理解它。它不是在删除堆栈中的节点,也不是正确地拉出双重节点。堆栈中的其他所有东西都可以正常工作。

double Stack::Pop()
{
   assert(!IsEmpty());

   double  n = topPtr.FirstNode();
   topPtr.DeleteNode( n );
   return n;
}
void LinkedList::DeleteNode( double x )
{
nodeptr prev, curr;

curr = start;

while( curr != NULL && x > curr->info )
{
    prev = curr;
    curr = curr->next;
}

if( x == curr->info )
{
    if( curr == start )
        start = start->next;
    else
        prev->next = curr->next;

    delete curr;
    count--;
}
}

//Below is where I use the pop function
roperand = rpnStack.Pop();
loperand = rpnStack.Pop();
cout << loperand << "  <Left      Right>  " << roperand << endl;
switch (op){
    case 0:
        answer = loperand + roperand;
        cout << answer << endl;
        rpnStack.Push(answer);
        break;
    case 1:
        answer = loperand - roperand;
        rpnStack.Push(answer);
        break;
    case 2:
        answer = loperand * roperand;
        rpnStack.Push(answer);
        break;
    case 3:
        answer = loperand / roperand;
        rpnStack.Push(answer);
        break;
            }

//This is the output I am getting

Please enter equation >> 5.5 5.5 +
Equation entered: 5.5 5.5 +
5<Left      Right>5
10
Below is what is left in the stack.
10
5.5
5.5

左5和右5应该都为5.5,并从堆栈中弹出,但它们仍留在其中,被拉出的值为5而不是5.5。所有变量的类型都是double。

0 个答案:

没有答案