温习一下数据结构,写了一个栈操作的模板... 第一次写的时候还忘了写析构函数,要吸取教训啊。 代码: #ifndef STACK_H #define STACK_H #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; template<class T> class Stack { public: int top; int maxtop; T *data; Stack(int size); //构造函数 ~Stack(); bool isEmpty(); //判断为空 bool isFull(); //判断为满 void push(T ch/*,Stack s*/); //压栈 T pop(); //出栈 }; template <class T> Stack<T>::Stack(int size) { maxtop=size; top=-1; data=new T[size]; } template <class T> Stack<T>::~Stack() { delete []data; } template <class T> bool Stack<T>::isEmpty() { return top<0; } template <class T> bool Stack<T>::isFull() { return top==maxtop; } template <class T> void Stack<T>::push(T ch/*,Stack s*/) { if (isFull()) { cout<<"The stack is full~"<<endl; return; } data[++top]=ch; } template <class T> T Stack<T>::pop() { if (isEmpty()) { cout<<"The stack is empty~"<<endl; return -1; } T s=data[top]; top--; return s; } #endif