STL-栈类模板

来源:互联网 发布:火炬之光2for mac汉化 编辑:程序博客网 时间:2024/03/29 02:11


#include
using namespace std;

template
class Stack
{
 private:
  int size;
  int top;
  T* space;
 public:
  Stack(int=10);
  ~Stack()
  {
   delete []space; 
  }
  bool push(const T&);
  T pop();
  bool IsEmpty() const
  {
   returntop==size; 
  }
  bool IsFull() const
  {
   returntop==0; 
  }
};
template
Stack ::Stack(int size)
{
 this->size=size;
 space=new T[size];
 top=size;
}
template
bool Stack ::push(const T& element)
{
 if(!IsFull())
 {
  space[--top]=element;
  returntrue; 
 }
 return false;
}
template
T Stack ::pop()
{
 return space[top++];
}
int main()
{
 StackS1(4);
 S1.push('x');
 S1.push('y');
 S1.push('z');
 S1.push('u');
 S1.push('v');
 while(!S1.IsEmpty())
  cout<<S1.pop()<<endl;
 return 0;
}

原创粉丝点击