Retroceder   Foros del Web > Temas generales de computación > Programación > C/C++

Respuesta
 
Herramientas Desplegado
Antiguo 24-abr-2008, 12:42   #1 (permalink)
nikolog ha deshabilitado el karma
 
Fecha de Ingreso: marzo-2008
Mensajes: 17
funcion crea nodo

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>


void main()
{
int z;
struct nodo
{
int x;
struct nodo *siguente;
};

struct nodo *p,*q,*L=NULL;



while(1)
{
printf("ingrese valor para lista");
scanf("%d",&z);
if(z==0)break;
p=L;
L=malloc(sizeof(struct nodo));
L->x=z;
L->siguente=p;
}
p=L;
while(p != NULL)
{
printf("%d->",q->x);
p=p->siguente;
}
getch();}



no me funciona la funcion para crear el nodo agredeseria alguna ayudita porfa




#include<stdio.h>
#include<stdlib.h>
#include<conio.h>



typedef struct node {
int x;
struct nodo *siguente;
}nodo;

typedef nodo *L;
typedef nodo *pnodo;




void crea_nodo(L *l,int y){
pnodo nuevo;
nuevo=(pnodo)malloc(sizeof(nodo));
nuevo->x=y;
if(l==NULL){
nuevo->siguente=*l;
*l=nuevo;}
else{
nuevo->siguente=*l;
*l=nuevo;}
}






void main()
{
L *l;
int z;

struct nodo
{

struct nodo *siguente;
};

struct nodo *p,*L=NULL;



while(1)
{
printf("ingrese valor para lista");
scanf("%d",&z);
if(z==0)break;
crea_nodo(&l,z);
}

getch();}
nikolog está desconectado   Responder Citando
Antiguo 24-abr-2008, 14:22   #2 (permalink)
Leber ha deshabilitado el karma
 
Fecha de Ingreso: marzo-2008
Mensajes: 19
Re: funcion crea nodo

Te pondre un ejemplo corto y sencillo para que entiendas el funcionamento:

Código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FF(str) str[strlen(str)-1]=0

struct lista
{
  char nombre[14];
  struct lista *sig;
};

struct lista *crear(struct lista *p);


int
main()
{

struct lista *L=NULL;  //<- para mantener la lista
struct lista *p;    //<- puntero para crear nuevos nodos


p=crear(p); //creamos nuevo nodo
p->sig=L;  //enlazamos el nodo->sig a la lista
L=p;       //hacemos que lista empieze por el nuevo nodo creado

fprintf(stdout,"Nodo contiene-> %s\n",p->nombre);

free (p);

return 0;

}

struct lista *crear(struct lista *p)
{

     if((p=(struct lista *)malloc(sizeof(struct lista)))==NULL)
       {
         perror("Error asignando memoria");
         exit(-1);
       }

      printf("Introduce nombre para nodo: ");
      fgets(p->nombre,13,stdin);
      FF(p->nombre);

  return p;  //devolvemos la estructura creada

}
Leber está desconectado   Responder Citando
Antiguo 25-abr-2008, 10:15   #3 (permalink)
nikolog ha deshabilitado el karma
 
Fecha de Ingreso: marzo-2008
Mensajes: 17
Re: funcion crea nodo

no entiendo muy bn tu codigo
nikolog está desconectado   Responder Citando
Antiguo 25-abr-2008, 16:37   #4 (permalink)
Mephisto está en el buen camino
 
Avatar de Mephisto
 
Fecha de Ingreso: enero-2006
Ubicación: Mexico DF
Mensajes: 181
Re: funcion crea nodo

Código:
nuevo->siguente=*l;
*l=nuevo;}
else{
nuevo->siguente=*l;
*l=nuevo;}
Quita los asteriscos, recuerda que el * es empleado para hacer referencia a lo que se apunta, las asignaciones que tienes son inconsistentes en tipo de dato...
__________________
Saludos...

Todos somos sabios, solo que en diferentes disciplinas...
Mephisto está desconectado   Responder Citando
Respuesta
No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Activado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 02:06.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93