Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/11/2011, 14:45
divavu
 
Fecha de Ingreso: octubre-2010
Mensajes: 2
Antigüedad: 13 años, 6 meses
Puntos: 0
Error en listas simples

Aparentemente esta todo bien, pero cuando pongo un numero mayor al que puse antes (en el for) no funciona.
No encuentro el error.

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

struct node
{
    int num;
    struct node *next;
};

struct node* allocatenode();
void ins_ord(struct node**,struct node *);
void mostrar(struct node *);


int main()
{
    struct node *head=NULL, *aux; // siempre head =NULL porque no hay nada al princicpio
    int i;

    for(i=1;i<=4;i++)
    {
        aux=allocatenode();
        printf("Ingrese numedio (0-10)\n");
        scanf("%d",&(aux->num));
        fflush(stdin);
        aux->next=NULL;
        ins_ord(&head,aux);
    }
    mostrar(head);
    return 0;
}

struct node *allocatenode()
{
    struct node *aux = (struct node*)malloc(sizeof(struct node));
    if (aux==NULL)
    {
        printf ("Falta de memoria\n");
        exit (-1);
    }
    return aux;
}

void ins_ord(struct node**headref,struct node*newnode)
{
    struct node * act, *ant;

    if (*headref==NULL) //en el caso de que no haya nada en la lista.
        *headref=newnode;
    else
    {
        ant=*headref;
        act=*headref;
        while(act!=NULL && newnode->num > act->num)     //empiezo por comparar los numeros
        {
            ant=act;
            act=act->next;
        }
        printf("%d,%d",act->num,newnode->num);
        if(ant==act)        //caso que no hata entrado al while nunca
        {
            newnode->next=*headref;
            *headref=newnode;
        }
        else
            if (act!=NULL)      //no llego al final, o sea encontro que act->num > newnode->num
            {
                printf("entro");
                newnode->next=act;
                ant->next=newnode;
            }
            else                    //act->num < newnode->num
                ant->next=newnode;
    }
}

void mostrar(struct node *c)
{
	if(c==NULL)
		printf("La lista está vacía\n");
	else
	{
		printf("La lista es:\n");
		while(c != NULL)
		{
			printf("%d\n",c->num);
			c=c->next ;
		}
	}
}