#pragma once
template<class T>
class MyStack
{
T* pBuff;
size_t capacity;
size_t len;
public:
MyStack()
{
pBuff = NULL;
capacity = len = 0;
}
~MyStack()
{
if (pBuff)
{
delete[] pBuff;
pBuff = NULL;
capacity = len = 0;
}
}
void Push(const T& data);//插入
void pop(void);//删除
T getTop();//获取栈顶元素
bool isEmpty();//判断栈是否为空
};
template<class T>
//入栈
void MyStack<T>::Push(const T& data)
{
if (len >= capacity)
{
capacity = capacity + (((capacity >> 1) > 1) ? (capacity >> 1) : 1);
T* pNew = new T[capacity];
if (pBuff)
{
memcpy(pNew, pBuff, sizeof(T) * len);
delete[] pBuff;
}
pBuff = pNew;
}
pBuff[len++] = data;
}
template<class T>
//出栈
void MyStack<T>::pop(void)
{
len--;
//释放内存
}
template<class T>
//获取栈顶元素
T MyStack<T>::getTop()
{
return pBuff[len - 1];
}
template<class T>
//判断栈是否为空
bool MyStack<T>::isEmpty()
{
return (len == 0);//len为0 空
}