Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/10/2009, 11:05
ger79
 
Fecha de Ingreso: octubre-2009
Mensajes: 3
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: problema con tp de c lista doblemente enlasada

aca hice otro cpdigo y tampoco me da ayuda por favor tengo que promocionar esta materia

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct tipo {
char codigo [8];
float precio;
};

struct nodo {
struct nodo *ant;
struct tipo datos;
struct nodo *sig;};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// /////
///// FUNCIONES /////
///// /////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


struct nodo *nuevonodo()
{
struct nodo *p=(struct nodo *) malloc (sizeof (struct nodo));

if (p==NULL)
{
printf ("MEMORIA INSUFICIENTE");
getch();
exit(0);
}
return p;
}

int listavacia (struct nodo *p)
{
return (p==NULL);
}

void vernodo (struct nodo *p)
{
clrscr();
printf("LA LISTA INGRESADA ES LA SIGUIENTE:\n");
printf("===================================\n\n");

while (p)
{
printf("***********\n");
printf("** %s\n",p->datos.codigo);
printf("** %.2f\n",p->datos.precio);
printf("***********\n\n\n");
p=p->sig;
}
}

struct nodo * inserta (struct tipo z,int pos,struct nodo *pri)
{
struct nodo *e,*act,*anter;
int postmp;

int k;

postmp = 0;
k=1;

act=pri;
anter = act->ant;

e=nuevonodo();
e->datos=z;

if(pos==0) //PRIMERO
{
pri->ant=e;
e->sig=pri;
e->ant=NULL;
pri=e;

}
else
{

while(k!=0)
{
if(postmp==pos)
{
if(act==NULL) //ULTIMO
{
e->sig=NULL;
e->ant=anter;
anter->sig=e;
k=0;
}
else //MEDIO
{
e->ant=act->ant;
e->sig=anter->sig;

anter->sig=e;
act->ant=e;
k=0;
}
}
else
{
act=act->sig;

if(act!=NULL)
{
anter=act->ant;
}
else
{
anter=anter->sig;
}

postmp++;
}
}
}
return pri;
}

struct nodo *creadoble (struct nodo *ult, struct nodo **pri, struct tipo x)
{
struct nodo *p;
p=nuevonodo ();
p->datos=x;

if (listavacia(ult))
{
p->sig=NULL;
p->ant=NULL;
ult=p;
*pri=p;
}
else
{
p->ant=ult;
p->sig=NULL;
ult->sig=p;
ult=p;
}
return ult;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// /////
///// MAIN /////
///// /////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void main(void)
{
clrscr ();

struct nodo * ult, * pri;
struct tipo pro;

int total;
int pos;
float aux;


ult = NULL;
pri = NULL;

total=0;

puts("\nIngrese c¢digo: (FIN para finalizar)");
fflush (stdin);

gets(pro.codigo);

while (strcmpi(pro.codigo,"FIN") != 0)
{
printf("\nIngrese precio:");
scanf ("%f",&aux);

pro.precio=aux;

ult=creadoble(ult,&pri,pro);

total++;

clrscr();
puts("\nIngrese c¢digo: (FIN para finalizar)");
fflush (stdin);

gets(pro.codigo);


}
if (listavacia(ult))
{
clrscr();
printf("\n No hay lista");
getch();
exit(0);
}
else
{
clrscr();
vernodo(pri);
printf("Por favor ingrese una posicion para insertar un nodo:\n");
scanf("%d",&pos);

if(pos<=total)
{
clrscr();
printf("Por favor ingrese codigo para el nuevo nodo:\n");
fflush (stdin);

gets(pro.codigo);

printf("Por favor ingrese un precio para el nuevo nodo:");
scanf("%f",&aux);
pro.precio=aux;

pri=inserta(pro,pos,pri);
}
else
{
clrscr();
printf("El valor ingresado no esta disponible en la lista por favor\ningrese un nuevo valor entre 0 y %d:",total);
scanf("%d",&pos);

if(pos<=total)
{
clrscr();
printf("Por favor ingrese codigo para el nuevo nodo:\n");
fflush (stdin);

gets(pro.codigo);

printf("Por favor ingrese un precio para el nuevo nodo:");
scanf("%f",&aux);
pro.precio=aux;

pri=inserta(pro,pos,pri);
}
else
{
clrscr();
printf("Te dije entre 0 y %d, ahora me cierro!! CHAU!!",total);
getch();
exit(0);
}
}



vernodo (pri);
};
getch ();
}