请教大神,c++输入字符串反序输出,为什么我的程序总是显示为空呢(stark is empty)。
感觉是Push和Pop的问题。请指导。
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
typedef int ElemType;
struct LSNodeType
{
ElemType data;
LSNodeType *next;
};
const int MAXSIZE=100;
class SqStack
{
private :
ElemType elem[MAXSIZE];
int top;
public:
SqStack(){top=-1;}
~SqStack(){}
void Push (ElemType e );
ElemType Pop();
int IsEmpty();
ElemType GetPop() {return elem[top];}
void Display();
void SetEmpty() {top=-1;}
};
void SqStack::Push(ElemType e)
{
if(top==MAXSIZE-1)
cout <<"stack overflow,cannot push"<<endl;
else
{
top++;
elem[top]=e;
}
}
ElemType SqStack::Pop()
{
top--;
return elem[top+1];
}
int SqStack::IsEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
void SqStack::Display ()
{
int k;
if (top==-1)
cout<<"stack is empty"<<endl;
else
{
cout<<"data elem:";
for(k=top;k>=0;k--)
cout<<setw(20)<<elem[k];
cout<<endl;
}
}
void main()
{
SqStack sq;
char e;
cout<<"put in 字符串e,反向输出"<<endl;
cin>>e;
sq.Push (e);
sq.Pop();
sq.IsEmpty();
sq.GetPop() ;
sq.Display();
sq.SetEmpty() ;
system("pause");
}