c++ - 链接列表打印不正确?

标签 c++ class linked-list

    struct stack_struct
    {
        int number;
        struct stack_struct *next_number;
    };

    stack_struct *mainStruct;

    class stack_class
    {
    private:

        struct stack_struct *head;

    public:
        stack_class();
        //~stack_class();
        void pushNumber(int number);
        void popANumber();
        void findNumber();
        void clearStack();
        void sizeFinder();
        void printStack();

    };

    stack_struct *pointerFunc,*pointerFunc2,*pointerFunc3,*printPointer;

    stack_class::stack_class()
    {
        head=NULL;
    }

    void stack_class::pushNumber(int numberFunc)
    {

        if(head==NULL)
        {
            head = new stack_struct;
            head->number = numberFunc;
            head->next_number = NULL;
            pointerFunc2=head;
        }
        else
        {
            pointerFunc = new stack_struct;
            pointerFunc->number=numberFunc;
            pointerFunc->next_number=NULL;
            head->next_number=pointerFunc;
            head=pointerFunc;
        }
    }

    void stack_class::printStack()
    {

                while(pointerFunc2)
                {
                    cout<<pointerFunc2->number<<endl;
                    pointerFunc2=pointerFunc2->next_number;
                }

    }

    int optionChosen;

    int main()
    {
        stack_class mainClassStack;
        do
        {

            cin>>optionChosen;

            switch(optionChosen)
            {
            case 1:
            {
                int pushInt;
                cout<<"\n\nEnter Number: ";
                cin >> pushInt;
                mainClassStack.pushNumber(pushInt);
                break;
            }
            case 2:
            {
                break;
            }
            case 3:
            {
                break;
            }
            case 4:
            {
                break;
            }
            case 5:
            {
                break;
            }
            case 6://print
            {
                mainClassStack.printStack();

                break;
            }
            default:
            {
                break;
            }
            }
        }while(optionChosen!=7);

        return 0;

我正在尝试使用动态内存(链表)实现堆栈类型的数据列表。但是,当我尝试打印列表时,它只打印列表一次,如果我尝试使用选项 6 再次重新打印,则就像列表消失了一样。我检查了代码两次,但无法找出问题所在。有什么建议吗?

最佳答案

您的代码的问题在于,在打印堆栈后,您没有将 pointerFunc2 重置为 head

正确重置它,或者在打印函数中使用局部变量。

这是函数的更正版本:

void stack_class::printStack()
{
    while (pointerFunc2)
    {
        cout << pointerFunc2->number << endl;
        pointerFunc2 = pointerFunc2->next_number;
    }

    // reset pointerFunc2 so the next iteration
    // can start at the head and print again.
    pointerFunc2 = head;
}

关于c++ - 链接列表打印不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15626862/

相关文章:

javascript - 根据 ul ID 将相同的类添加到子元素

php - 如何自动加载扩展类?

c - 为什么这个使用单指针的程序不起作用

c - 遍历链表并将每个值打印到屏幕上 C

c++ - Catch 单元测试订购

c++ - 我怎样才能用多态来实现这个?

c++ - 头文件中模板类实现的模板成员

c - 链表使用中的段错误C

c++ - 运行时的虚函数调用(std c++)

c++ - 有没有一种访问函数外信息的好方法?