栈类模板实现如下,
- 栈类模板
#ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED #include#include using namespace std; template class Stack{ public: Stack(int capacity); virtual ~Stack(); //清栈 void clearStack(); //入栈 bool push(T element); //出栈 bool pop(T &element); //判空 const bool isEmpty(); //判满 const bool isFull(); //栈长 const int length(); //列栈 void printStack(void(*pFunc)(T), bool fromTop = true); private: //栈数组指针 T *m_pStack; //栈容量 int m_iCapacity; //栈顶 int m_iTop; //栈长 int m_iLength; }; template Stack ::Stack(int capacity){ clearStack(); m_iCapacity = capacity; if ((m_pStack = new T[m_iCapacity]) == NULL) { throw string("Stack Initialization Failed!"); } } template Stack ::~Stack(){ delete []m_pStack; m_pStack = NULL; } template void Stack ::clearStack(){ m_iTop = 0; m_iLength = 0; } template bool Stack ::push(T element){ if (isFull()) { return false; } m_pStack[m_iTop] = element; m_iLength++; m_iTop++; return true; } template bool Stack ::pop(T &element){ if (isEmpty()) { return false; } m_iLength--; m_iTop--; element = m_pStack[m_iTop]; return true; } template const bool Stack ::isEmpty(){ return m_iTop == 0 ? true : false; } template const bool Stack ::isFull(){ return m_iTop == m_iCapacity ? true : false; } template const int Stack ::length(){ return m_iLength; } template void Stack ::printStack(void (*pFunc)(T), bool fromTop){ if (fromTop) { //从顶 for (int i = (m_iLength - 1); i >= 0; i--) { pFunc(m_pStack[i]); } } else { //从底 for (int i = 0; i < m_iLength; i++) { pFunc(m_pStack[i]); } } } #endif // STACK_H_INCLUDED
- Coordinate测试类
#ifndef COORDINATE_H_INCLUDED
#define COORDINATE_H_INCLUDED
class Coordinate{
public:
Coordinate();
Coordinate(double x, double y);
virtual ~Coordinate();
double getX();
double getY();
private:
double m_iX;
double m_iY;
};
Coordinate::Coordinate(){
}
Coordinate::Coordinate(double x, double y){
m_iX = x;
m_iY = y;
}
Coordinate::~Coordinate(){
}
double Coordinate::getX(){
return m_iX;
}
double Coordinate::getY(){
return m_iY;
}
#endif // COORDINATE_H_INCLUDED
- 测试代码
#include#include "Stack.h" #include "Coordinate.h" using namespace std; void printChar(char o){ cout << o; } void printCoor(Coordinate o){ cout << "(" << o.getX() << "," << o.getY() << ")" << endl; } int main() { try{ cout << "*************char*************" << endl; Stack stackChar = Stack (5); //入栈 cout << stackChar.length() << endl; stackChar.push('h'); stackChar.push('e'); stackChar.push('l'); stackChar.push('l'); stackChar.push('o'); stackChar.push('!'); //栈满插入失败 cout << stackChar.length() << endl; cout << "--------------------------" << endl; //打印 stackChar.printStack(&printChar); cout << endl; stackChar.printStack(&printChar, false); cout << endl; cout << stackChar.length() << endl; cout << "--------------------------" << endl; //出栈测试 char e1,e2; stackChar.pop(e1); stackChar.pop(e2); cout << "i'm out : " << e1 << endl; cout << "i'm out : " << e2 << endl; stackChar.printStack(&printChar); cout << endl; cout << stackChar.length() << endl; cout << "*************Coordinate*************" << endl; Stack *stackCoor = new Stack (3); if (stackCoor == NULL) { cout << "Point init failed!" << endl; return 3; } //入栈 Coordinate c1 = Coordinate(1,2); Coordinate c2 = Coordinate(3,4); Coordinate c3 = Coordinate(5,6); Coordinate c4 = Coordinate(7,8); //栈满插入失败 stackCoor->push(c1); stackCoor->push(c2); stackCoor->push(c3); stackCoor->push(c4); cout << "--------------------------" << endl; //打印 stackCoor->printStack(&printCoor); cout << endl; stackCoor->printStack(&printCoor, false); cout << endl; cout << stackCoor->length() << endl; cout << "--------------------------" << endl; //出栈 Coordinate c5; stackCoor->pop(c5); cout << "i'm out : "; printCoor(c5); stackCoor->printStack(&printCoor); cout << stackCoor->length() << endl; delete stackCoor; stackCoor = NULL; } catch (string &e) { //捕获可预知异常 cout << e << endl; return 2; } catch (...) { //捕获其他所有未知异常 return 1; } return 0; }
- 运行结果



