jueves, 29 de marzo de 2012

Implementación del tipo pila usando celdas enlazadas

Aquí tenemos el código de la implementación de un TAD tipo pila usando celdas enlazadas. Se podrían incluir más funciones en dicha implementación.

#ifndef __PILA_HPP__
#define __PILA_HPP__
#include "pilainterfaz.hpp"
#include
#include


using namespace std;
namespace ed
{
template
struct Nodo
{
G Data;
Nodo *antiguo;
};

template
class Pila:public PilaInterfaz
{
private:
int _tamanyo;
int _limite;
Nodo *_cima;
public:
Pila(int m)
{
tamanyo(0);
limite(m);
_cima=NULL;
}
Pila &operator=(const Pila &x) throw()
{

int i;
G v[x.tamanyo()];
tamanyo(0);
limite(x.limite());
Nodo *aux;
aux= new Nodo;
aux=x._cima;
for(i = 0; i < x.tamanyo(); i++)
{
v[i]=aux->Data;
aux=aux->antiguo;
}
for(i=tamanyo()-1; i >=0; i--)
{
apilar(v[i]);
}
return *this;
}
void apilar(const G &n) throw()
{
assert(tamanyo() < limite());
Nodo *siguiente;
siguiente= new Nodo;
siguiente->Data=n;
siguiente->antiguo=_cima;
_cima=siguiente;
tamanyo(tamanyo()+1);
}

void desapilar() throw ()
{
assert(not estaVacia());
Nodo *aux=_cima;
_cima=_cima->antiguo;
delete aux;
tamanyo(tamanyo()-1);
}
const G & cima () const throw ()
{
assert(not estaVacia());
return _cima->Data;
}
bool estaVacia () const throw ()
{
if (tamanyo()==0)
return true;
else
return false;
}
bool estaLlena () const throw ()
{
if (tamanyo()==limite())
return true;
else
return false;
}
int limite() const throw()
{
return _limite;
}
int tamanyo() const throw()
{
return _tamanyo;
}
void limite(int m) throw()
{
_limite=m;
}
void tamanyo(const int &x) throw()
{
_tamanyo=x;
}
};
}
#endif