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 再次重新打印,它就像列表消失了。我检查了两次代码,但无法弄清楚问题所在。有什么建议么?