Título: Duda TDA LiastaDobles Publicado por: Soulrata en 26 Abril 2012, 14:54 pm //Mi duda es sobre lista doblemente enlazadas... porque no me deja poner null a mi nodo crear, y donde hay mas errores...? gracias!
//Soulrata #include<stdio.h> #include<stdlib.h> struct nodo { int info; struct nodo *sig,*ant; }; //////////////////////////////////////////////////////// void Crear (struct nodo **p) { *p = (struct nodo*) malloc (sizeof(struct nodo)); *p = NULL; (*p)->sig = (*p)->ant = NULL; printf("****** Crear ******\n"); printf("****** NODO *p ****** %d \n", *p); printf("****** NODO sig ****** %d \n", (*p)->sig); printf("****** NODO ant ****** %d \n\n", (*p)->ant); /*******************************/ } //////////////////////////////////////////////////////// void AltaPrin(struct nodo **p, int dato) { struct nodo *aux; aux = (struct nodo*) malloc (sizeof(struct nodo)); aux->info = dato; aux->sig = (*p)->sig; aux->ant = *p; if((*p)->sig!=NULL) { ((*p)->sig)->ant = aux; (*p)->sig = aux; printf("****** IF ******\n"); } //*p = aux; printf("****** AltaPrin ******\n"); printf("****** NODO *p ****** %d \n", (*p)); printf("****** NODO INFO ****** %d \n", (*p)->info); printf("****** SIGUIENTE ****** %d \n", (*p)->sig); printf("****** ANTERIOR ****** %d \n\n", (*p)->ant); /*******************************/ } //////////////////////////////////////////////////////// void AltaFin (struct nodo **p, int dato) { struct nodo *aux, *aux2; aux = (struct nodo*) malloc (sizeof(struct nodo)); aux2 = (struct nodo*) malloc (sizeof(struct nodo)); aux->info = dato; aux->sig = NULL; if(!(*p)) { *p = aux; (*p)->ant = NULL; printf("****** Fin1 ******\n"); } else { aux2 = *p; while(aux2->sig != NULL) aux2->sig; aux2->sig = aux; aux->ant = aux2; printf("****** Fin2 ******\n"); } printf("****** AltaFin ******\n"); printf("****** NODO *p ****** %d \n", *p); printf("****** NODO INFO ****** %d \n", (*p)->info); printf("****** SIGUIENTE ****** %d \n", (*p)->sig); printf("****** ANTERIOR ****** %d \n\n\n", (*p)->ant); /*******************************/ } //////////////////////////////////////////////////////// void Recorrer(struct nodo *p) { printf("****** Recorrido del nodo ******\n"); while(p) { printf("Valor: %d\n", p->info); p = p->sig; } /*******************************/ } void ImprimeLista(struct nodo *p) { struct nodo *aux = p; if(aux==NULL) printf("La lista esta vacia.\n"); else{ while(aux!=NULL){ printf("%s -->",aux->info); aux=aux->sig; } printf("NULL\n\n"); } } //////////////////////////////////////////////////////// void BorrarUnNodo (struct nodo **p, int dato) { struct nodo *aux, *auxAnt; aux = *p; while(aux&&aux->info!=dato) aux = aux->sig; if(aux) { auxAnt = aux->ant; if(!auxAnt) { *p = (*p)->sig; (*p)->ant = NULL; free(aux); } else if (!aux->sig) { auxAnt->sig = NULL; free(aux); } else { auxAnt->sig = aux->sig; (aux->sig)->ant = auxAnt; free(aux); } } /*******************************/ } //////////////////////////////////////////////////////// void Destruir (struct nodo **p) { if(*p!=NULL) free(*p); if((*p)->sig == NULL) { printf("****** Nodo Destruido NULL *******\n"); } } //////////////////////////////////////////////////////// /////******* MAIN *******///// int main() { struct nodo *a; Crear(&a); AltaPrin(&a,12); AltaPrin(&a,13); AltaPrin(&a,14); AltaPrin(&a,15); ImprimeLista(a); AltaFin(&a, 2); BorrarUnNodo(&a,4); Recorrer(a); /*******************************/ return 0; } //*******************************// Título: Re: Duda TDA LiastaDobles Publicado por: Ferno en 26 Abril 2012, 15:56 pm No entiendo por que estás pidiéndole memoria para un struct nodo* y luego lo haces apuntar a NULL (en la función Crear). Estarías perdiendo la memoria que acababas de pedir!
|