栈顺序存储结构利用一个大数组,相当于简化了的线性表,线性表具有查找、插入、删除等功能,而栈则简化为只包括压入、弹出这种更有针对性的功能。下面是自己写的栈的顺序存储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