141 lines
3.2 KiB
C++
141 lines
3.2 KiB
C++
#pragma once
|
|
#define GARRAY_INCREMENT 100
|
|
//cola generico---------------------------------
|
|
//+cosas por le final
|
|
//-por el inicio
|
|
template <typename T>
|
|
struct Element_Cgcola
|
|
{
|
|
T t;
|
|
Element_Cgcola<T> *ant;
|
|
Element_Cgcola<T> *sig;
|
|
};
|
|
template <typename T>
|
|
class Cgcola
|
|
{
|
|
public:
|
|
//variables----------------------------------------------------------------------------------
|
|
int n; //numero de elementos en array
|
|
Element_Cgcola<T> *ini; //inicio del array
|
|
Element_Cgcola<T> *fin; //fin del array
|
|
//**************************************************************
|
|
//constructores-------------------------------------------------------------------------------
|
|
//**************************************************************
|
|
Cgcola()
|
|
{
|
|
ini=fin=NULL;
|
|
n=0;
|
|
};
|
|
//**************************************************************
|
|
~Cgcola()
|
|
{
|
|
borra();
|
|
};
|
|
//**************************************************************
|
|
//funciones-----------------------------------------------------------------------------------
|
|
//**************************************************************
|
|
virtual inline T* operator++()//da puntero al ultimo elemento
|
|
{
|
|
if (fin)
|
|
return &fin->t;
|
|
return NULL;
|
|
|
|
};
|
|
//**************************************************************
|
|
virtual inline T* operator--()//da puntero al primero
|
|
{
|
|
if(ini)
|
|
return &ini->t;
|
|
return NULL;
|
|
};
|
|
//**************************************************************
|
|
virtual inline BOOL operator+(T &e )//añade al final
|
|
{
|
|
if (fin)
|
|
{
|
|
fin->sig=(Element_Cgcola<T>*)malloc(sizeof(Element_Cgcola<T>));
|
|
if (!fin->sig)
|
|
return FALSE;
|
|
fin->sig->ant=fin;
|
|
fin=fin->sig;
|
|
}
|
|
else
|
|
{
|
|
fin=(Element_Cgcola<T>*)malloc(sizeof(Element_Cgcola<T>));
|
|
if (!fin)
|
|
return FALSE;
|
|
ini=fin;
|
|
fin->ant=NULL;
|
|
}
|
|
fin->t=e;
|
|
fin->sig=NULL;
|
|
n++;
|
|
return TRUE ;
|
|
};
|
|
//**************************************************************
|
|
virtual inline BOOL operator-(T &e) //añade al principio
|
|
{
|
|
if (ini)
|
|
{
|
|
ini->ant=(Element_Cgcola<T>*)malloc(sizeof(Element_Cgcola<T>));
|
|
if (!ini->ant)
|
|
return FALSE;
|
|
ini->ant->sig=ini;
|
|
ini=ini->ant;
|
|
}
|
|
else
|
|
{
|
|
ini=(Element_Cgcola<T>*)malloc(sizeof(Element_Cgcola<T>));
|
|
if (!ini)
|
|
return FALSE;
|
|
fin=ini;
|
|
ini->sig=NULL;
|
|
}
|
|
ini->t=e;
|
|
ini->ant=NULL;
|
|
n++;
|
|
return TRUE ;
|
|
};
|
|
//**************************************************************
|
|
virtual inline BOOL operator+=(T* e )//da el ultimo u lo elimina
|
|
{
|
|
if (!fin)
|
|
return FALSE;
|
|
*e=fin->t;
|
|
Element_Cgcola<T> *el=fin;
|
|
fin=fin->ant;
|
|
fin->sig=NULL;
|
|
if (!fin)
|
|
ini=fin;
|
|
free(el);
|
|
n--;
|
|
return TRUE ;
|
|
};
|
|
//**************************************************************
|
|
virtual inline BOOL operator-=(T* e )//da el primero y lo elimina
|
|
{
|
|
if (!ini)
|
|
return FALSE;
|
|
*e=ini->t;
|
|
Element_Cgcola<T> *el=ini;
|
|
ini=ini->sig;
|
|
if (!ini)
|
|
fin=ini;
|
|
else
|
|
ini->ant=NULL;
|
|
free(el);
|
|
n--;
|
|
return TRUE ;
|
|
};
|
|
//**************************************************************
|
|
virtual inline void borra()//libera la memoria del array
|
|
{
|
|
while(ini)
|
|
{
|
|
fin=ini->sig;
|
|
free(ini);
|
|
ini=fin;
|
|
}
|
|
}
|
|
//**************************************************************
|
|
}; |