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

#include<iostream>
using namespace std;
const int maxstack=10;
template <class T>
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 <class T>
ArrayStack<T>::ArrayStack()
/*Pre: None
  Post: The stack is initialized to be empty.*/
{	  
	current=-1;
}
template <class T>
void ArrayStack<T>::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<maxstack-1)
	   array[++current]=item;	
}
template <class T>
void ArrayStack<T>::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 <class T>
T& ArrayStack<T>::top()
/*Pre:None
  Post:If the stack is not empty,the top of the ArrayStack is returned.*/
{
     if(!IsEmpty())
	   return array[current];	 
}
template <class T>
bool ArrayStack<T>::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 <class T>
bool ArrayStack<T>::full() const
/*Pre:None
  Post:If the stack is full,true is returned.Otherwise,false is returned*/
{
	return current==9;
}
template <class T>
int ArrayStack<T>::size() const
/*Pre:None
  Post:Return the number of entries in the stack.*/
{
	return (current+1);
}
template <class T>
void ArrayStack<T>::clear()
/*Pre:None
  Post:Reset the stack to be empty*/
{
	current=-1;
}

	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值