|
JStack
#include "JStack.h"
template <class Etype>
JStack<Etype>::JStack()
{
m_pTOS = NULL;
}
template <class Etype>
JStack<Etype>::~JStack()
{
empty();
}
template <class Etype>
const Etype &
JStack<Etype>::peek() const
{
if ( m_pTOS == NULL )
throw "JStack is Empty";
return m_pTOS->value;
}
template <class Etype>
Etype
JStack<Etype>::pop()
{
if ( m_pTOS == NULL )
throw "JStack is Empty";
node* old = m_pTOS;
m_pTOS = m_pTOS->next;
const Etype temp = old->value;
delete old;
return temp;
}
template <class Etype>
void
JStack<Etype>::empty()
{
while ( m_pTOS != NULL )
{
node* old = m_pTOS;
m_pTOS = m_pTOS->next;
delete old;
}
}
template <class Etype>
void
JStack<Etype>::push( const Etype &item )
{
m_pTOS = new node( item, m_pTOS );
}
template <class Etype>
int
JStack<Etype>::search( const Etype &item ) const
{
const node* pJStack = m_pTOS;
for( int count = 1; pJStack != NULL; count++ )
{
if ( pJStack->value == item )
return count;
pJStack = pJStack->next;
}
return -1;
}
|
|
|