Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表打印不正确? struct stack\u struct { 整数; 结构堆栈\u结构*下一个\u编号; }; 堆栈结构*主结构; 类堆栈 { 私人: 结构堆栈_结构*头; 公众: 堆栈_类(); //~stack_class(); 无效数字(整数); void popANumber(); void findNumber(); void clearStack(); void sizeFinder(); void printStack(); }; 堆栈结构*pointerFunc、*pointerFunc2、*pointerFunc3、*printPointer; 堆栈类::堆栈类() { head=NULL; } void stack_class::pushNumber(int numberFunc) { if(head==NULL) { head=新堆栈结构; head->number=numberFunc; head->next_number=NULL; pointerFunc2=头部; } 其他的 { pointerFunc=新堆栈结构; pointerFunc->number=numberFunc; pointerFunc->next_number=NULL; head->next_number=pointerFunc; head=指针函数; } } void stack_class::printStack() { while(pointerFunc2) { 选择了coutopion; 开关(选装软管) { 案例1: { int-pushInt; 库特·普辛特; mainclasstack.pushNumber(pushInt); 打破 } 案例2: { 打破 } 案例3: { 打破 } 案例4: { 打破 } 案例5: { 打破 } 案例6://打印 { mainclasstack.printStack(); 打破 } 违约: { 打破 } } }while(optionChosen!=7); 返回0;_C++_Class_Linked List - Fatal编程技术网

C++ 链表打印不正确? struct stack\u struct { 整数; 结构堆栈\u结构*下一个\u编号; }; 堆栈结构*主结构; 类堆栈 { 私人: 结构堆栈_结构*头; 公众: 堆栈_类(); //~stack_class(); 无效数字(整数); void popANumber(); void findNumber(); void clearStack(); void sizeFinder(); void printStack(); }; 堆栈结构*pointerFunc、*pointerFunc2、*pointerFunc3、*printPointer; 堆栈类::堆栈类() { head=NULL; } void stack_class::pushNumber(int numberFunc) { if(head==NULL) { head=新堆栈结构; head->number=numberFunc; head->next_number=NULL; pointerFunc2=头部; } 其他的 { pointerFunc=新堆栈结构; pointerFunc->number=numberFunc; pointerFunc->next_number=NULL; head->next_number=pointerFunc; head=指针函数; } } void stack_class::printStack() { while(pointerFunc2) { 选择了coutopion; 开关(选装软管) { 案例1: { int-pushInt; 库特·普辛特; mainclasstack.pushNumber(pushInt); 打破 } 案例2: { 打破 } 案例3: { 打破 } 案例4: { 打破 } 案例5: { 打破 } 案例6://打印 { mainclasstack.printStack(); 打破 } 违约: { 打破 } } }while(optionChosen!=7); 返回0;

C++ 链表打印不正确? struct stack\u struct { 整数; 结构堆栈\u结构*下一个\u编号; }; 堆栈结构*主结构; 类堆栈 { 私人: 结构堆栈_结构*头; 公众: 堆栈_类(); //~stack_class(); 无效数字(整数); void popANumber(); void findNumber(); void clearStack(); void sizeFinder(); void printStack(); }; 堆栈结构*pointerFunc、*pointerFunc2、*pointerFunc3、*printPointer; 堆栈类::堆栈类() { head=NULL; } void stack_class::pushNumber(int numberFunc) { if(head==NULL) { head=新堆栈结构; head->number=numberFunc; head->next_number=NULL; pointerFunc2=头部; } 其他的 { pointerFunc=新堆栈结构; pointerFunc->number=numberFunc; pointerFunc->next_number=NULL; head->next_number=pointerFunc; head=指针函数; } } void stack_class::printStack() { while(pointerFunc2) { 选择了coutopion; 开关(选装软管) { 案例1: { int-pushInt; 库特·普辛特; mainclasstack.pushNumber(pushInt); 打破 } 案例2: { 打破 } 案例3: { 打破 } 案例4: { 打破 } 案例5: { 打破 } 案例6://打印 { mainclasstack.printStack(); 打破 } 违约: { 打破 } } }while(optionChosen!=7); 返回0;,c++,class,linked-list,C++,Class,Linked List,我正在尝试使用动态内存(链表)实现数据列表的堆栈类型。但是当我尝试打印列表时,它只打印一次列表,如果我尝试使用选项6再次打印,则列表就不存在了。我检查了两次代码,但都不知道问题出在哪里。有什么建议吗?您的代码的问题是,在打印堆栈后,您没有重置pointerFunc2来到头部 请正确重置它,或者在打印函数中使用局部变量 以下是您的函数的更正版本: struct stack_struct { int number; struct stack_struc

我正在尝试使用动态内存(链表)实现数据列表的堆栈类型。但是当我尝试打印列表时,它只打印一次列表,如果我尝试使用选项6再次打印,则列表就不存在了。我检查了两次代码,但都不知道问题出在哪里。有什么建议吗?

您的代码的问题是,在打印堆栈后,您没有重置
pointerFunc2
来到
头部

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

以下是您的函数的更正版本:

    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;
void stack_class::printStack()
{
while(pointerFunc2)
{
下一个号码的密码;
}
//重置pointerFunc2以便下一次迭代
//可以从打印头开始重新打印。
pointerFunc2=头部;
}

第二次打印不起作用的原因是您使用的是全局变量而不是局部变量:

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;
}
当您在函数或类外部声明变量时,该变量将变为全局变量。全局变量在函数调用后仍然有效,并保留上次在其中设置的值。要成为局部变量,需要在
printStack
内部声明一个变量,初始化为堆栈的头部,然后在退出函数时丢弃该变量n、
pointerFunc
也是如此


printPointer
pointerFunc3
变量未使用,可以从源代码中删除。

问题在于首次使用此选项打印堆栈时

pointerFunc2=pointerFunc2->下一个\u编号


pointerFunc2成为第一次迭代后的最后一个元素。这就是为什么您认为它已经消失了。您应该在打印后将pointerFunc2重置为指向head节点。因此,请先保存它,然后在遍历整个列表后恢复它,以便下次打印堆栈时,您将从head节点开始。

非常感谢在很大程度上,您的答案给出了使用不同变量来打印函数的想法。我使用了第三个变量,并将
pointerFunc2
保留在原来的位置。Thanx作为提示,+1glad我可以提供帮助。通常您可以使用gdb来了解发生了什么。这是一个非常强大的工具!
stack_struct *pointerFunc2;