#ifndef STRUCTSTACK
#define STRUCTSTACK
#include<iostream>
#include<vector>
using namespace std;
int counter=0;
/***************定义错误类*****************/
class illegal
{
public:
illegal(){};
};
/****************定义基类,内部都是虚函数*****************/
template<class T>
class Linearlist
{
public:
virtual ~Linearlist(){};
virtual bool empty() const=0;
virtual T& get(int theIndex) const =0;
virtual int indexof(const T& theElementof) =0;
virtual void erase(int theIndex) =0;
virtual void insert(int theIndex,const T& theElement)=0;
virtual void output(ostream &out)=0;
};
/*************用基类Linearlist定义arrayList**************/
template<class T>
class Arraylist : public Linearlist<T>
{
public:
Arraylist(int Maxsize){}
private:
T* element;
int arraylength;
int listsize;
};
/*****************节点类定义******************/
template<class T>
struct ChainNode
{
ChainNode<T> *next;
T element;
ChainNode(){}
ChainNode(const T& element)
{this->element=element;}
ChainNode(const T& element,ChainNode<T> *next)
{
this->element=element;
this->next=next;
}
};
/******************抽象栈定义**********************/
template<class T>
class stack
{
public:
virtual ~stack() {}
virtual bool empty()=0;
virtual void push(const T& theElement)=0;
virtual void pop()=0;
virtual int size()=0;
virtual T& top()=0;
};
/********************数组栈定义******************/
template<class T>
class stackarray : public stack<T>
{
public:
stackarray(int Initalsize=10)
{
stacksize=Initalsize;
array=new T[Initalsize];
stacktop=-1;
}
~stackarray(){delete [] array;}
bool empty(){return stacktop==-1;}
int size()
{
return stacktop+1;
}
T& top()
{
if(stacktop==-1)
throw illegal();
else
return array[stacktop];
}
void push(const T& theElement);
void pop()
{
if(stacktop==-1)
throw illegal();
array[stacktop--].~T();
}
private:
int stacksize;
int stacktop;
T *array;
};
template<class T>
void stackarray<T>::push(const T& element)
{
if(stacktop+1==stacksize)
{
delete [] array;
array=new T[stacksize*2];
stacksize*=2;
}
array[++stacktop]=element;
}
/********************链表栈定义******************/
template<class T>
class stacklist : public stack<T>
{
public:
stacklist(){stacktop=NULL;stacksize=0;}
~stacklist();
bool empty()
{return stacksize==0;}
int size(){return stacksize;}
T& top()
{
if(stacksize==0)
throw illegal();
else
return stacktop->element;
}
void pop();
void push(const T& element)
{
stacktop=new ChainNode<T>(element,stacktop);
stacksize++;
}
private:
ChainNode<T> *stacktop;
int stacksize;
};
template<class T>
void stacklist<T>::pop()
{
ChainNode<T> text;
if(stacksize==0)
throw illegal();
else
text=stacktop->next;
delete stacktop;
stacktop=text;
stacksize--;
}
template<class T>
stacklist<T>::~stacklist()
{
ChainNode<T> *nextnode;
while(stacktop!=NULL)
{
nextnode=stacktop->next;
delete stacktop;
stacktop=nextnode;
}
}
#endif // STRUCTSTACK