elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Duda TDA LiastaDobles
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Duda TDA LiastaDobles  (Leído 1,133 veces)
Soulrata

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Duda TDA LiastaDobles
« 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;
}

//*******************************//


En línea

Ferno


Desconectado Desconectado

Mensajes: 375


Ver Perfil
Re: Duda TDA LiastaDobles
« Respuesta #1 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!


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Pequeña duda con un comando batch (NUEVA DUDA RELACIONADA)
Scripting
revenge1252 9 9,740 Último mensaje 13 Febrero 2008, 21:41 pm
por revenge1252
Duda duda y duda de Metasploit
Bugs y Exploits
huber_nomas 4 5,776 Último mensaje 17 Febrero 2012, 14:00 pm
por MauroMasciar
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines