Thread: Problem with dynamic lists

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20

    Problem with dynamic lists

    Found the problem, it's in the list creation, I have something that's creating a loop in the list:
    Code:
    int insere_col_inicial(lista_coelhos *base,int id_colonia){    lista_coelhos *aux,*procura,*seguinte;
        int tamanho;
        int x;
        int y;
    
    
        x = rand()*1.0*MAX_X/RAND_MAX;
        y = rand()*1.0*MAX_X/RAND_MAX;
        tamanho = ceil(abs((x -y)%30))+1;/*To avoid size zero of the colonies*/    
        
        aux=(lista_coelhos*)calloc(1,sizeof(lista_coelhos));
        if(aux==NULL)
        {
            return -1;
        }    
    
    
        aux->id_colonia=id_colonia;
        aux->x=x;
        aux->y=y;
        aux->tamanho=tamanho;
    
    
        procura=base;
        seguinte=base->prox;
    
    
        while(seguinte != NULL && tamanho>seguinte->tamanho)
        {
            procura=procura->prox;
            seguinte=seguinte->prox;
        }
    
    
        aux->prox=seguinte;
        procura->prox=aux;
    
    
        return 0;
    }
    here's where I call the function:
    Code:
    for(i=1;i<=100;i++) /*To insert inicial colonies*/
        {
            nova_id=id_colonia+1;
            insere_col_inicial(base,nova_id);
            id_colonia=nova_id;
        }
    Last edited by Chinchila; 12-13-2012 at 09:08 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Chinchila View Post
    Found the problem
    Excellent! Erm... what is your problem?

    In any post like this always say (i) what you expect to happen and (ii) what act
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    The problem is, I want to create a list organized by tamanho (it's one of my variables), but something is making a loop in the list, when I reach the end of the list it has a pointer to an element in the middle and I don't understand why

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The only obvious problem I see is that this function does not allow you to insert at the head of the list.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Portugal
    Posts
    20
    The head, you mean the end or the beggining? It's supposed to have the first element in blank, because of the searches

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Dynamic Lists? Is it possible? Is it a logical solution?
    By arcaine01 in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2009, 01:08 AM
  3. From Python's Linked Lists to Dynamic Arrays
    By hexbox in forum C Programming
    Replies: 3
    Last Post: 01-26-2005, 03:14 PM
  4. Dynamic number of linked lists
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 09:30 PM
  5. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM