1

到目前为止我的代码是......

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 popNumber();
    void findNumber();
    void clearStack();
    void sizeFinder();
    void printStack();
    void reverseStack();//Extra Credit
};
stack_class mainClassStack;

stack_struct *pointerFunc,*tailPointer=NULL,*pointerFunc3,*printPointer;

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

stack_class::~stack_class()
{
    clearStack();
    cout<<"\nList Cleared.\n";
    system("pause");
}


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;
        }
    }

}

只要不是最后一个数字,弹出就可以正常工作。如果弹出最后一个数字(列表为空),并且我尝试打印列表,则程序会无限打印垃圾。如果我在列表为空后尝试弹出一个数字,程序会冻结。我该如何解决这些问题?

4

3 回答 3

0

包括 cstdlib 以退出

    int Stack::pop()
    {   if (top == NULL)
            {
             cerr <<"Stack is empty";
             exit(1);
            }
        else
            {   
             Node* zap = top;
             top = zap->link;
             int return_value = zap->n;
             delete zap;
             return return_value;
            }
     }

Qazi 你必须if(top==NULL)在这两个程序的开始处进行检查。

于 2013-03-30T17:11:37.093 回答
0

像这样改变你的 popNumber(),

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 if(head->next_number==NULL)
    {
        cout<<"\nThe Poped element is"<<head->number;
        head=NULL;
    }
    else
    {
       // Your Code;    
    }

}
于 2013-03-30T04:20:53.707 回答
0

您也可以始终创建一个虚拟节点来解决此问题。否则:

int stack_class::popNumber()
{
    if(head==NULL)
        throw new Exception("Stack is empty");
    int result = head->number;
    delete head;
    head = head->next_number;
    return result;
}

void stack_class::pushNumber(int number)
{
    stack_struct *elem= new stack_struct();
    elem->number = number;
    elem->next_number = head;
    head = elem;
}

!!!!!尽量不要使用全局变量,如 (stack_class mainClassStack;

stack_struct *pointerFunc,*tailPointer=NULL,)

于 2013-03-30T04:28:25.293 回答