//栈-5
//南有乔木
//2018/10/17
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<cstdlib>
#define ERROR 0
#define OK 1
#define SElemType int
#define STACK_INIT_SIZE 50
#define STACK_INCR_SIZE 10
using namespace std;
template<class any>
class Stack
{
protected:
int top;
any* base;
int stacksize;
public:
Stack():top(0), stacksize(STACK_INIT_SIZE)//初始化链表
{
base = new any[stacksize];
if(!base)
{
cout<<"内存分配失败"<<endl;
exit(OVERFLOW);
}
else
cout<<"初始化成功"<<endl;
}
Stack(int size):top(0), stacksize(size)
{
base = new any[stacksize];
if(!base)
{
cout<<"内存分配失败"<<endl;
exit(OVERFLOW);
}
else
cout<<"初始化成功"<<endl;
}
~Stack()//销毁链表
{
delete[] base;
cout<<endl<<"成功销毁!"<<endl;
}
bool Empty_Stack();//销毁链表
void Clear_Stack();//清空链表
void Push_Stack(any e);//插入元素e
void Pop_Stack();//删除栈顶元素
void Visit_Stack();//访问栈,从栈顶到栈底
any Get_Top()//获取栈顶
{
if(top!=0)
return base[top];
else
cout<<"栈空..."<<endl;
exit(OVERFLOW);
}
int Size_Stack();//获取元素个数,即栈的长度
};
template<class any>
bool Stack<any>::Empty_Stack()
{
if(top==0)
{
return true;
}
else
return false;
}
template<class any>
void Stack<any>::Clear_Stack()
{
top = 0;
}
template<class any>
void Stack<any>::Push_Stack(any a)
{
if(top+1<=stacksize)
{
base[++top] = a;
}
else
{
cout<<"......";
cout<<"栈满..."<<endl;
exit(1);
}
}
template<class any>
void Stack<any>::Pop_Stack()
{
if(top>0)
{
top--;
}
else
{
cout<<"栈空..."<<endl;
exit(1);
}
}
template<class any>
int Stack<any>::Size_Stack()
{
return top;
}
template<class any>
void Stack<any>::Visit_Stack()
{
int t=top;
while(t>0)
{
cout<<base[t]<<" ";
t--;
}
cout<<endl;
}