Introduction
This is a small stack implementation that is relied on MFC CArray template class. It has implemented the three mostly needed stack functions Push, Pop and Peek. The class is directly derived from CArray and consequently it supplies all inherited functions too.
Usage sample
CStack<int> m_stack;
m_stack.Push( 1973 );
m_stack.Push( 2004 );
m_stack.Push( 30 );
int size = m_stack.GetSize();int value = m_stack.Peek(); int value = m_stack.Pop();
The implementation
#ifndef _H_TEMPLATE_H_
#define _H_TEMPLATE_H_
template <class T> class CStack : public CArray<T,T>
{
public:
void Push( T newView ){
Add( newView );
}
T Peek(int index=-1){
return ( index >= GetSize() ||
GetSize()==0) ? NULL : ElementAt( ( index==-1?GetSize()-1:index) );
}
T Pop(){
T item = Peek();
if(item) RemoveAt(GetSize()-1);
return item;
}
};
#endif
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here