您的位置:首页 > 编程语言 > C语言/C++

栈顺序存储结构的C++模板类程序源代码

2016-05-31 21:38 375 查看
栈顺序存储结构利用一个大数组,相当于简化了的线性表,线性表具有查找、插入、删除等功能,而栈则简化为只包括压入、弹出这种更有针对性的功能。下面是自己写的栈的顺序存储C++模板类源代码:

//linearstack.h
#ifndef LINEARSTACK
#define LINEARSTACK
#include <IOSTREAM>
const int ROOMSIZE=100;
template<class Type>
class LinearStack
{
private:
Type Data[ROOMSIZE];
int top;
public:
LinearStack():top(0){};
~LinearStack(){};
bool Push(Type temp);
Type Pop();
bool IsFull();
bool IsEmpty();
void Print();
};
template<class Type>
bool LinearStack<Type>::Push(Type temp)
{
if(IsFull())
return false;
Data[top++]=temp;
return true;
}
template<class Type>
Type LinearStack<Type>::Pop()
{
if(IsEmpty())
return Type(-111);
top--;
return Data[top];
}
template<class Type>
bool LinearStack<Type>::IsFull()
{
if(top==ROOMSIZE)
return true;
else
return false;
}
template<class Type>
bool LinearStack<Type>::IsEmpty()
{
if(top==0)
{
return true;
}
else
return false;
}
template<class Type>
void LinearStack<Type>::Print()
{
for(int i=0;i<top;i++)
{
std::cout<<Data[i]<<" ";
}
std::cout<<std::endl;
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息