从堆栈列表中弹出一个数字(链表)? [英] popping out a number from a stack list (linked list)?

查看:130
本文介绍了从堆栈列表中弹出一个数字(链表)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class stack_class 
{
private:

struct stack_struct * head;

public:
stack_class();
〜stack_class();
void pushNumber(int number);
void popNumber();
void findNumber();
void clearStack();
void sizeFinder();
void printStack();

};

void stack_class :: popNumber()
{
stack_struct * pointerPop = NULL,* pointerPop2 = NULL;
int popCounter = 0,i = 0;
pointerPop2 = tailPointer;
if(head == NULL)
{
cout<<\\\
No Member to Delete.\\\
;
}
else
{
while(pointerPop2)
{
popCounter ++;
// cout<<< pointerFunc3-> number<< endl;
pointerPop2 = pointerPop2-> next_number;
}
pointerPop = tailPointer;
while(i <(popCounter-2))
{
pointerPop = pointerPop-> next_number;
i ++;
}
pointerPop-> next_number = NULL;
delete head;
head = pointerPop;
}

}

void stack_class :: printStack()
{
pointerFunc3 = tailPointer;
if(tailPointer == NULL)
{
cout<<\ nNo List.\\\
中的成员;
}
else
{
cout<<\\\
\\\
List is:\\\
;
while(pointerFunc3)
{
cout<<< pointerFunc3-> number<< endl;
pointerFunc3 = pointerFunc3-> next_number;
}
}

}

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

这是我的代码,问题是,当我POP最后一个数字并尝试打印列表,它进入一个无限循环并打印垃圾。当我在列表中的所有内容被删除后按删除选项,程序冻结。任何建议为什么这是这样的行为,我如何解决它?

解决方案

永远不是假的。根据你如何实现你的next_number(如果它是一个循环链表),我相信你应该改为:

  

如果你想让它循环遍历所有的节点,则可以使用(pointerPop2!= head)



<

class stack_class
{
private:

    struct stack_struct *head;

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

};

void stack_class::popNumber()
{
    stack_struct *pointerPop=NULL,*pointerPop2=NULL;
    int popCounter=0,i=0;
    pointerPop2=tailPointer;
    if(head==NULL)
    {
        cout<<"\nNo Member to Delete.\n";
    }
    else
    {
        while(pointerPop2)
        {
            popCounter++;
            //cout<<pointerFunc3->number<<endl;
            pointerPop2=pointerPop2->next_number;
        }
        pointerPop=tailPointer;
        while(i<(popCounter-2))
        {
            pointerPop=pointerPop->next_number;
            i++;
        }
        pointerPop->next_number=NULL;
        delete head;
        head=pointerPop;
    }

}

void stack_class::printStack()
{
    pointerFunc3=tailPointer;
    if(tailPointer==NULL)
    {
        cout<<"\nNo Members in List.\n";
    }
    else
    {
        cout<<"\n\nList Is:\n";
        while(pointerFunc3)
        {
            cout<<pointerFunc3->number<<endl;
            pointerFunc3=pointerFunc3->next_number;
        }
    }

}

constructor to class

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

This is my code, the problem with it is, when I POP the very last number and try to print the list, it enters an infinite loop and prints garbage. when I press the delete option after everything in the list has been deleted, the program freezes. Any suggestions why this is acting like this??And how do I fix it?

解决方案

The condition of your first while loop will never be false. Depending on how you've implemented your next_number (if it's a circular linked list), I believe you should change it to:

while(pointerPop2 != head)

if you want it to loop through all nodes.

这篇关于从堆栈列表中弹出一个数字(链表)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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