64,676
社区成员
发帖
与我相关
我的任务
分享
template <class Type>
class Stack
{
private:
enum {MAX = 10};
Type item[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item);
bool pop(Type & item);
};
template <class Type>
Stack<Type>::Stack()
{
top=0;
}
template <class Type>
bool Stack<Type>::isempty()
{
return top==0;
}
template <class Type>
bool Stack<Type>::isfull()
{
return top==MAX;
}
template <class Type>
bool Stack<Type>::push(const Type & item1)
{
if(top<MAX)
{
item[top++]=item1;
return true;
}
else
{
throw("over flow!");
return false;
}
}
template <class Type>
bool Stack<Type>::pop(Type & item)
{
if(top>0)
{
item=item[--top];
return true;
}
else
{
throw("over flow!");
return false;
}
}
int main(void)
{
Stack<string> st;
string s;
cout<<"Please input a string!\n";
cin>>s;
st.push(s);
if(!st.isempty()) cout<<"push hase successed\n";
st.pop(s);
if(st.isempty()) cout<<"pop hase successed\n";
return 250;
}