Código
// Autor: Namida // Fecha: 04 enero 2011 /// DIRECTIVAS GENERALES DEL PREPROCESADOR #define _list_ #ifndef NULL #define NULL 0 #endif #define OFF false #define ON true ///CODIGO // Declaraciones Previas template<typename TYPE> class list; template<typename TYPE> list<TYPE> &operator < (list<TYPE> &L,TYPE X); template<typename TYPE> list<TYPE> &operator > (list<TYPE> &L,TYPE X); template<typename TYPE> list<TYPE> &operator ~ (list<TYPE> &L); // Declaracion template<typename TYPE> class list { private: /*Definicion del nodo*/ struct node{ TYPE data; node* next_node; }; /*Informacion sobre la lista*/ node* first_node; unsigned list_size; bool delete_mode; public: /*Constructor de clase*/ list(); /*Operadores*/ //Escritura friend list<TYPE> &operator < <TYPE>(list<TYPE> &L,TYPE X); friend list<TYPE> &operator > <TYPE>(list<TYPE> &L,TYPE X); //Lectura TYPE operator [] (unsigned N); //Borrado friend list<TYPE> &operator ~ <TYPE>(list<TYPE> &L); /*Descriptor*/ unsigned Size(); }; // Metodos /*Constructor*/ template<typename TYPE> list<TYPE>::list(){ first_node=NULL; list_size=0; delete_mode=OFF; }; /*Operadores*/ //Escritura template<typename TYPE> list<TYPE> &operator < (list<TYPE> &L,TYPE X){ list<TYPE>::node* new_node=new list<TYPE>::node; new_node->data=X; new_node->next_node=NULL; if (L.first_node!=NULL){ list<TYPE>::node* aux_node=L.first_node; while (aux_node->next_node!=NULL){ aux_node=aux_node->next_node; }; aux_node->next_node=new_node; }else{ L.first_node=new_node; }; L.list_size+=1; return L; }; template<typename TYPE> list<TYPE> &operator > (list<TYPE> &L,TYPE X){ list<TYPE>::node* new_node=new list<TYPE>::node; new_node->data=X; new_node->next_node=L.first_node; L.first_node=new_node; L.list_size+=1; return L; }; //Lectura template<typename TYPE> TYPE list<TYPE>::operator [] (unsigned N){ try{ if (N<list_size){ switch (delete_mode){ case OFF: { node* aux_node=first_node; for(unsigned i=0;i<N;i++){ aux_node=aux_node->next_node; }; return aux_node->data; }; case ON: { if (N==0){ node* aux_node=first_node; first_node=aux_node->next_node; delete aux_node; }else{ node* aux_node=first_node; for(int i=0;i<int(N)-1;i++){ aux_node=aux_node->next_node; }; node* delete_node=aux_node->next_node; aux_node->next_node=aux_node->next_node->next_node; delete delete_node; }; list_size-=1; delete_mode=OFF; throw 0; }; }; }else{ throw 0; }; }catch(int code){ TYPE X=TYPE(); return X; }; }; //Borrado template<typename TYPE> list<TYPE> &operator ~ (list<TYPE> &L){ L.delete_mode=ON; return L; }; /*Descriptor*/ template<typename TYPE> unsigned list<TYPE>::Size(){ return list_size; }; #undef OFF #undef ON
Un ejemplo de su uso
[/size]Código
#include"list.h" int main(int argc,char* argv[]){ //Creacion de una lista tipo int list<int> L=list<int>(); /* Agregar datos A la cola*/ L<0<1<2<3<4; /*A la cabeza*/ L>5>6; //Estado actual de la lista: 6-5-0-1-2-3-4 //Leer informacion de la lista L[1]; //Devuelve el numero 5 //Borrado (~L)[1]; //Elimina el numero 5 de la lista L.Size(); //Devuelve el tamagno de la lista return 0; };