用顺序结构(数组)与模板技术实现Stack如下:
View Code
const int MAXSTACK = 10;
template<class T>
class ZtkStack{
public:
ZtkStack();
bool empty() const;
ErrorCode pop();
ErrorCode top(T &item) const;
ErrorCode push(const T &item);
private:
int count;
T entry[MAXSTACK];
};
template<class T>
ZtkStack<T>::ZtkStack():count(0)
{
}
template<class T>
bool ZtkStack<T>::empty() const
{
bool outcome = true;
if(count > 0)
outcome = false;
return outcome;
}
template<class T>
ErrorCode ZtkStack<T>::pop()
{
ErrorCode outcome = success;
if(count>0)
--count;
else
outcome = underflow;
return outcome;
}
template<class T>
ErrorCode ZtkStack<T>::push(const T &item)
{
ErrorCode outcome = success;
if(count < MAXSTACK)
entry[count++] = item;
else
outcome = overflow;
return outcome;
}
template<class T>
ErrorCode ZtkStack<T>::top(T &item) const
{
ErrorCode outcome = success;
if(count > 0)
item = entry[count - 1];
else
outcome = underflow;
return outcome;
}
资料:C++编译器不能支持对模板的分离式编译, http://blog.csdn.net/pongba/article/details/19130原文链接: https://www.cnblogs.com/zhoutk/archive/2012/10/16/2725328.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/65928
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!