一、 结果显示
- 堆栈实现测试 Test.h
- 表达式运算: Expression.h
二、 ArrayStack.h
#include<iostream>
using namespace std;
const int Max_size = 128;
template<typename T>
class ArrayStack
{
public:
ArrayStack()
{
count = 0;
array = new T[Max_size];
};
~ArrayStack()
{
if(count == 0)
{
delete array;
cout<<endl;
cout<<"栈为空析构"<<endl;
}
else
{
while(count > 0)
this->pop();
delete array;
cout<<endl;
cout<<"清除栈内元素析构"<<endl;
}
};
T pop() //弹栈
{
if(count < 0 )
{
cout<<endl;
cout<<"弹栈时:栈内元素数量有误!"<<endl;
}
if(count == 0)
{
cout<<endl;
cout<<"弹栈时:栈为空!"<<endl;
}
else
return array[(count--)-1];
}
void push(T data) //推栈
{
if(count < 0 )
{
cout<<endl;
cout<<"推栈时:栈内元素数量有误!"<<endl;
}
else
array[(++count)-1] = data;
}
int size() //栈内元素个数
{
return count;
}
bool isEmpty() //判断栈内是否为空
{
return count == 0?1:0;
}
T top() //返回栈顶元素
{
if(count == 0)
cout<<"栈顶:栈为空栈!!!"<<endl;
else
return array[count-1];
}
void Show()
{
if(count<0)
{
cout<<endl;
cout<<"Show:堆栈内元素个数有误!"<<endl;
}
if(count == 0)
{
cout<<endl;
cout<<"Show:堆栈为空!"<<endl;
}
else
{
int i = count;
cout<<endl;
cout<<"Show:堆栈内元素为:"<<"[";
for(; i>1; i--)
cout<<array[count-i]<<",";
cout<<array[count-1];
cout<<"]"<<endl;
}
}
int count;
protected:
T *array;
};
三、 LinkStack.h
#include<iostream>
using namespace std;
template<typename T>
class LinkStack
{
public:
int count;
struct Node
{
T data;
Node *next;
};
LinkStack()
{
count = 0;
head = new Node;
head->next = NULL;
};
~LinkStack()
{
if(count =