template<typename T>
class Stack
{
public:
Stack(int s = 10);
~Stack();
public:
T top();
void push(T t);
T pop();
bool empty();
int size();
private:
int count;
int capacity;
T* array;
};
template<typename T>
Stack<T>::Stack(int s)
{
count = 0;
capacity = s;
array = new T[capacity];
}
template<typename T>
Stack<T>::~Stack()
{
delete[] array;
array = NULL;
}
template<typename T>
T Stack<T>::top()
{
if (count <= 0)
{
return NULL;
}
return array[count - 1];
}
template<typename T>
void Stack<T>::push(T t)
{
if (count >= capacity)
return;
array[count++] = t;
}
template<typename T>
T Stack<T>::pop()
{
if (count <= 0)
{
return NULL;
}
return array[--count];
}
template<typename T>
bool Stack<T>::empty()
{
return count == 0 ? true : false;
}
template<typename T>
int Stack<T>::size()
{
return count;
}
Stack 的C++实现
最新推荐文章于 2024-06-26 13:41:53 发布