#include <iostream>
#include <new>
using namespace std;
template< class T >
class PILA_DATOS{
template< class t > friend class PILA;
public:
template< class U > friend ostream& operator<< ( ostream &,
const PILA_DATOS< U > & );
PILA_DATOS< T > &operator= ( PILA_DATOS< T > & );
private:
T nodo;
PILA_DATOS< T > *ptrPilaSiguiente;
};
template< class U > ostream& operator<< ( ostream &salida,
const PILA_DATOS< U > &a ){
salida << a.nodo << "->";
PILA_DATOS< U > *b = a.ptrPilaSiguiente;
while ( b != NULL ){
salida << b->nodo << "->";
b = b->ptrPilaSiguiente;
}
salida << "NULL" << endl;
return salida;
}
template< class T >
PILA_DATOS< T >& PILA_DATOS< T >::operator= ( PILA_DATOS< T > &a ){
nodo = a.nodo;
ptrPilaSiguiente = a.ptrPilaSiguiente;
return *this;
}
template< class T >
class PILA{
public:
PILA ();
~PILA ();
PILA< T > &empujarPila ( const T& );
PILA< T > &sacarPila ();
PILA< T > &mostrarPila ();
template< class U > friend ostream& operator<< ( ostream &,
const PILA< U > & );
private:
PILA_DATOS< T > *ptrPila;
bool estaVacia ();
};
template< class T >
PILA< T >::PILA (){
ptrPila = NULL;
}
template< class T >
PILA< T >::~PILA (){
while ( ptrPila != NULL ){
PILA_DATOS< T > *temp = ptrPila;
ptrPila = ptrPila->ptrPilaSiguiente;
delete temp;
}
}
template< class T >
bool PILA< T >::estaVacia (){
return ( ptrPila == NULL ? true : false );
}
template< class T >
PILA< T > &PILA< T >::empujarPila ( const T &a ){
if ( estaVacia () ){
ptrPila = new PILA_DATOS< T >;
ptrPila->nodo = a;
ptrPila->ptrPilaSiguiente = NULL;
}
else{
PILA_DATOS< T > *nuevaPila;
nuevaPila= new PILA_DATOS< T >;
nuevaPila->nodo = a;
nuevaPila->ptrPilaSiguiente = ptrPila;
ptrPila = nuevaPila;
}
return *this;
}
template< class T >
PILA< T > &PILA< T >::sacarPila (){
PILA_DATOS< T > *temp;
temp = ptrPila;
ptrPila = ptrPila->ptrPilaSiguiente;
delete temp;
return *this;
}
template< class T >
PILA< T > &PILA< T >::mostrarPila (){
PILA_DATOS< T > *a = ptrPila;
while ( a != NULL ){
cout << a->nodo << "->";
a = a->ptrPilaSiguiente;
}
cout << "NULL" << endl;
return *this;
}
template< class U > ostream& operator<< ( ostream &salida,
const PILA< U > &a ){
salida << *( a.ptrPila );
return salida;
}
int main (){
PILA< float > *a = new PILA< float >;
cout << *a;
getchar();
return 0;
}