Foros del Web » Programación para mayores de 30 ;) » C/C++ »

funcion crea nodo

Estas en el tema de funcion crea nodo en el foro de C/C++ en Foros del Web. #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 ...
  #1 (permalink)  
Antiguo 24/04/2008, 12:42
 
Fecha de Ingreso: marzo-2008
Mensajes: 17
Antigüedad: 16 años, 1 mes
Puntos: 0
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();}
  #2 (permalink)  
Antiguo 24/04/2008, 14:22
 
Fecha de Ingreso: marzo-2008
Mensajes: 37
Antigüedad: 16 años, 1 mes
Puntos: 0
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

}
  #3 (permalink)  
Antiguo 25/04/2008, 10:15
 
Fecha de Ingreso: marzo-2008
Mensajes: 17
Antigüedad: 16 años, 1 mes
Puntos: 0
Re: funcion crea nodo

no entiendo muy bn tu codigo
  #4 (permalink)  
Antiguo 25/04/2008, 16:37
Avatar de Mephisto  
Fecha de Ingreso: enero-2006
Ubicación: Mexico DF
Mensajes: 184
Antigüedad: 18 años, 4 meses
Puntos: 3
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...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:13.