使用节点实现重新填充堆栈 [英] Refill Stack using Node Implementation

查看:165
本文介绍了使用节点实现重新填充堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我把它全部关闭后,我很难再次填充堆叠,以便将其打印出来。我使用节点实现所以我认为这个事实是什么让我困惑。任何建议都会感谢,谢谢。



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

  //打印Gumball信息字段(颜色和计数器)的函数
void Stack :: print()
{
Node * p;
键入x;

while(top!= NULL){
p = top;
x = p - >获取信息();
cout<< < x.color<< < < x.counter < endl endl;
top = p - > getnext();
}

return;
}

这是我试图循环并存储在temp中的堆栈。它编译但仍然不工作作为假设

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

while(top!= NULL){
p = top;
x = p - >获取信息();
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;
}


< div:stack = print 显示一个当前的snapshop你的堆栈,所以它没有业务修改任何堆栈的成员变量。没有必要做一个备份的堆栈和恢复它。



而不是让顶部成员变量向下遍历堆栈,初始化局部节点指针与 top 相同,



换句话说, top 应该是只读(不可变的) code> print 成员函数。要强制成员函数不能修改任何成员变量,可以通过在成员函数声明的末尾添加 const 关键字来使成员函数不可变。



示例:

  // Const成员函数强制堆栈保持未修改
void Stack :: print()const
{
Node * p = top; //使p最初指向堆栈的顶部
键入x;

while(p!= NULL){
x = p - >获取信息();
cout<< < x.color<< < < x.counter < endl endl;
p = p-> getNext(); //使p沿着堆栈向下到达下一个节点。
}
}


I'm having a hard time refilling the stack after i take it all off in order to print it out. I am using node implementation so i think this fact is what is confusing me. Any suggestions would be appreciated, thank you.

This is my original stack::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;
}

解决方案

Stack::print shows a current "snapshop" of your stack, so it has no business modifying any of Stack's member variables. There's no need to make a "backup" of the stack and to restore it. You just have to walk down the stack in a manner that doesn't disturb the stack.

Instead of making the top member variable walk down the stack, initialize a local Node pointer to be the same as top and make the local pointer walk down the stack.

In other words, top should be read-only (immutable) within your print member function. To enforce that a member function must not modify any member variables, you can make the member function immutable by adding the const keyword at the end of your member function declaration.

Example:

// Const member function enforces that the Stack remain unmodified
void Stack::print() const
{
    Node *p = top; // Make p initially point to the top of the stack
    Type x;

    while(p != NULL) {
        x = p -> getinfo();
        cout << " " << x.color << " " << " " << x.counter << endl << endl;
        p = p->getNext(); // Make p walk down the stack to the next Node.
    } 
}

这篇关于使用节点实现重新填充堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆