c++模板类堆栈
#ifndef __MyConsole__Stack__
#define __MyConsole__Stack__
#include <iostream>
template<class T>
class Stack{
public:
explicit Stack(int size);
~Stack();
bool IsEmpty();
int CurrentSize();
bool Push(const T& item);
bool IsFull();
void ShowStack();
bool Pop(T *pResult);
private:
int top;
int maxSize;
T *data;
};
using std::cout;
using std::cin;
using std::endl;
template<class T>
Stack<T>::Stack(int size){
top = 0;
maxSize = size;
data = new T[maxSize];
}
template<class T>
Stack<T>::~Stack(){
delete []data;
}
template<class T>
bool Stack<T>::IsEmpty(){
return top == 0;
}
template<class T>
int Stack<T>::CurrentSize(){
return top;
}
template<class T>
bool Stack<T>::Push(const T& item){
if (IsFull()) {
returnfalse;
}
data[top++] = item;
returntrue;
}
template<class T>
bool Stack<T>::Pop(T *pResult)
{
if (IsEmpty()) {
returnfalse;
}
*pResult = data[--top];
returntrue;
}
template<class T>
bool Stack<T>::IsFull(){
return top == maxSize;
}
template<class T>
void Stack<T>::ShowStack(){
cout << "Some informations about Stack as follows:" << endl;
cout << "CurrentSize = " << top << " MaxSize = " << maxSize << endl;
for (int i = 0; i < top; ++i) {
cout << data[i] << " ";
if (i % 4 == 0 && i != 0) {
cout << endl;
}
}
cout << endl;
}
#endif /* defined(__MyConsole__Stack__) */