头文件:
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
class OutOfBound{};
template <class elemType>
class Stack
{
public:
virtual bool isEmpty() const=0;
virtual void push(const elemType &x)=0;
virtual elemType pop()=0;
virtual elemType top() const=0;
virtual ~Stack(){}
};
template <class elemType>
class seqStack:public Stack<elemType>
{
private:
elemType *elem;
int Top;
int maxSize;
void doubleSpace();
public:
seqStack(int initSize=10);
~seqStack();
bool isEmpty() const;
elemType top() const;
elemType pop();
void push(const elemType &x);
};
template <class elemType>
class linkStack:public Stack<elemType>{
private:
struct node{
elemType data;
node *next;
node(const elemType &x,node *N=NULL){data=x; next=N;}
node():next(NULL){}
~node(){}
};
node *Top;
public:
linkStack();
~linkStack();
bool isEmpty() const;
elemType top() const;
elemType pop();
void push(const elemType &x);
};
#include "seqStack.cpp"
#include "linkStack.cpp"
#endif // STACK_H_INCLUDED
顺序实现:
#ifndef cpp1_h
#define cpp1_h
#include <iostream>
#include "stack.h"
using namespace std;
template <class elemType>
seqStack<elemType>::seqStack(int initSize){
//declaration could have default value
elem=new elemType[initSize];
maxSize=initSize;
Top=-1;
}
template <class elemType>
seqStack<elemType>::~seqStack(){
delete [] elem;
}
template <class elemType>
bool seqStack<elemType>::isEmpty() const{
return Top==-1;
}
template <class elemType>
elemType seqStack<elemType>::top() const{
if (Top==-1) throw OutOfBound();
return elem[Top];
}
template <class elemType>
elemType seqStack<elemType>::pop(){
if (Top==-1) throw OutOfBound();
return elem[Top--];
}
template <class elemType>
void seqStack<elemType>::push(const elemType &x){
if(Top==maxSize-1) doubleSpace();
elem[++Top]=x;
}
template <class elemType>
void seqStack<elemType>::doubleSpace(){
elemType *tmp=elem;
elem=new elemType[2*maxSize];
for(int i=0;i<maxSize;i++) elem[i]=tmp[i];
maxSize*=2;
delete [] tmp;
}
#endif // cpp1_h
链表实现:
#ifndef cpp2_h
#define cpp2_h
#include <iostream>
#include "stack.h"
using namespace std;
template <class elemType>
linkStack<elemType>::linkStack(){Top=NULL;}
template <class elemType>
linkStack<elemType>::~linkStack(){
node *tmp;
while(Top!=NULL){
tmp=Top;
Top=Top->next;
delete tmp;
}
}
template <class elemType>
bool linkStack<elemType>::isEmpty() const{
return Top==NULL;
}
template <class elemType>
elemType linkStack<elemType>::pop(){
node *tmp=Top;
elemType x=Top->data;
Top=Top->next;
delete tmp;
return x;
}
template <class elemType>
elemType linkStack<elemType>::top() const{
if (Top==NULL) throw OutOfBound();
return Top->data;
}
template <class elemType>
void linkStack<elemType>::push(const elemType &x){
node *tmp=new node(x,Top);
Top=tmp;
}
#endif // cpp2_h
主函数(测试用):
#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
seqStack<int> st1(5);
st1.push(1);
st1.push(2);
st1.push(3);
cout<<st1.top();
st1.pop();
st1.pop();
st1.pop();
if (st1.isEmpty()) cout<<"yes";
cout<<endl;
linkStack<int> st2;
st2.push(1);
st2.push(2);
cout<<st2.pop();
cout<<st2.pop();
cout<<st2.isEmpty();
return 0;
}