如何使用节点实现重新填充堆栈

时间:2011-11-02 02:18:20

标签: c++ stack using nodes fill

  

可能重复:
  Refill Stack using Node Implementation

我把它全部取下以便打印出来后,我很难重新填充堆栈。我正在使用节点实现,所以我认为这个事实让我感到困惑。任何建议将不胜感激,谢谢。

这是我原来的堆栈:: print()

// Function to print Gumball info field (color and counter)
void Stack::print()
{
    Node *p;
    Type x;

    while(top != NULL) {
        p = top;
        x = p -> getinfo();
        cout << " " << x.color << " " << " " << x.counter << endl << endl;
        top = p -> getnext();
    } 

    return;
}

This is my stack with my attempt to loop and store in temp. It compiles but still is not working as suppose to

void Stack::print()
{
    Node *p,*q;
    Type x,x_temp;

    while(top != NULL) {
        p = top;
        x = p -> getinfo();
        cout << " " << x.color << " " << " " << x.counter << endl << endl;
        x_temp = x;
        q = top;
        top = p -> getnext();
    }

    while(top != NULL) {
        q -> setinfo(x_temp);
        q -> setnext(top);
        top = q;
    }
    return;
}

// Function to print Gumball info field (color and counter) void Stack::print() { Node *p; Type x; while(top != NULL) { p = top; x = p -> getinfo(); cout << " " << x.color << " " << " " << x.counter << endl << endl; top = p -> getnext(); } return; }

0 个答案:

没有答案