data structure--Stack(基于数组实现)

#include
using namespace std;
const int maxstack=10;
template 
class ArrayStack
{
public:
		ArrayStack();
		T& top();
		void pop();
		void push(T&);
		bool IsEmpty()  const;
		bool full() const;
		void clear();
		int size() const;
private:
	    int current;
		T array[maxstack];		
};
template 
ArrayStack::ArrayStack()
/*Pre: None
  Post: The stack is initialized to be empty.*/
{	  
	current=-1;
}
template 
void ArrayStack::push(T &item)
/*Pre:None
  Post:If the stack is not full,item is added to the top of the ArrayStack. Else,the stack is left unchanged.*/
{
	if(current
void ArrayStack::pop()
/*Pre:None
  Post:If the stack is not empty,the top of the ArrayStack is removed. Else,the stack is left unchanged.*/
{
	if(!IsEmpty())
	   current--;
}
template 
T& ArrayStack::top()
/*Pre:None
  Post:If the stack is not empty,the top of the ArrayStack is returned.*/
{
     if(!IsEmpty())
	   return array[current];	 
}
template 
bool ArrayStack::IsEmpty() const
/*Pre:None
  Post:If the stack is empty,true is returned.Otherwise,false is returned*/
{	  	
   if(current==-1)
      return 1;
   else 
    return 0;
}
template 
bool ArrayStack::full() const
/*Pre:None
  Post:If the stack is full,true is returned.Otherwise,false is returned*/
{
	return current==9;
}
template 
int ArrayStack::size() const
/*Pre:None
  Post:Return the number of entries in the stack.*/
{
	return (current+1);
}
template 
void ArrayStack::clear()
/*Pre:None
  Post:Reset the stack to be empty*/
{
	current=-1;
}

	

你可能感兴趣的:(c++,数据结构,stack,structure)