Thread: Queue List with infinit loop

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    53

    Queue List with infinit loop

    Hi....again

    I'm having a little problem with an infinite loop, all works normally except the print function who never stop

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct eng
    {
        char nomes[100];
        char curso[100];
        char inst[100];
        int nota;
        char curri[100];
    }INFO;
    
    
    typedef struct elemento
    {
        INFO nodo;
        struct elemento *seguinte;
        struct elemento *anterior;
    }ELEM;
    
    
    int enqueue(INFO info, ELEM **initLista, ELEM **fimLista)
    {
        ELEM *novo;
        novo=(ELEM*)malloc(sizeof(ELEM));
        if(novo==NULL)
        {
            printf("Erro na lista");
            return -1;
        }
        novo->nodo=info;
        novo->seguinte=NULL;
        novo->anterior=NULL;
        if(*initLista==NULL)
        {
            *initLista=novo;
            *fimLista=novo;
        }
        novo->anterior=novo;
        (*fimLista)->seguinte=*fimLista;
        *fimLista=novo;
        return 0;
    }
    
    
    int print(ELEM *initLista)
    {
        ELEM *aux;
        if(initLista==NULL){
                           printf("lista vazia!\n");
                           return -1;
                           }
        aux=initLista;
        for(aux=initLista;aux!=NULL;aux=aux->seguinte)
        {
            printf("%s %s %s %d %s\n",aux->nodo.nomes,aux->nodo.curso,aux->nodo.inst,aux->nodo.nota, aux->nodo.curri);
            aux=aux->seguinte;
        }
        return 0;
    }
    
    
    int listarMaiorCatorze(ELEM *initLista)
    {
        ELEM *aux;
        aux= initLista;
        if(aux->nodo.nota>14)
        {
            printf("%s  %d", aux->nodo.nomes,aux->nodo.nota);
            aux=aux->seguinte;
        }
        return 0;
    }
    int nextOnList(ELEM *initLista)
    {
        //printf(initLista->seguinte);
        
        return 0;
    }
    int main()
    {
        ELEM *initLista=NULL, *fimLista=NULL;
        INFO info;
        int op;
        char buf[30];
        do
        {
            printf("Name: \n");
            fgets(info.nomes, 100, stdin);
            printf("Degree: \n");
            fgets(info.curso, 100, stdin);
            printf("Institution: \n");
            fgets(info.inst, 100, stdin);
            printf("Grade: \n");
            fgets(buf, 100, stdin);
            sscanf(buf, "%d", &info.nota);
            printf("Curriculum: \n");
            fgets(info.curri, 100, stdin);
            printf("More records? (0-to leave) \n");
            scanf("%d", &op);
            getchar();
        }
        while(op!=0);
        enqueue(info,&initLista, &fimLista);
        print(initLista);
        printf("\nCandidatos com mais de 14: \n");
        listarMaiorCatorze(initLista);
        return 0;
    }
    The print function works with inifinite loop.

    If i want to retrieve the next candidate, what do i have to do??

    Regards

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    novo->anterior=novo;
    (*fimLista)->seguinte=*fimLista;
    *fimLista=novo;

    These should be in an else block, and changed to
    Code:
    else {
      // not an empty list
      novo->anterior=*fimLista;  // points back to the current tail
      (*fimLista)->seguinte=nova; // (old) tail points to the new node
      *fimLista=novo;  // tail updated to point to new end node
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The reason that your print function doesn't work is because your list is broken. I think...

    All of the nodes are supposed to be connected. If you want to add to the tail end of a list you have to get the last node then connect the next pointer and prev pointer.

    Code:
    ELEM* tail;
    for (tail = initLista; tail->seguinte != NULL; tail = tail->next) 
        /* do nothing */;
    tail->seguinte = novo;
    novo->anterior = tail;
    novo->seguinte = NULL;
    This should work for inserting all list nodes at the end. The only special case is a new list where there are no nodes.

    I really hope I got everything right since I don't speak your language. Let me know if there is any more I can do and please try to explain what you can.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Hi Salem
    Thanks

    Now i have a segmentation fault in:

    Code:
    int print(ELEM *initLista)
    {
        ELEM *aux;
        if(initLista==NULL){
                           printf("lista vazia!\n");
                           return -1;
                           }
        aux=initLista;
        for(aux=initLista;aux!=NULL;aux=aux->seguinte)
        {
            printf("%s %s %s %d %s\n",aux->nodo.nomes,aux->nodo.curso,aux->nodo.inst,aux->nodo.nota, aux->nodo.curri);
            aux=aux->seguinte;
        }
        return 0;
    }
    for(aux=initLista;aux!=NULL;aux=aux->seguinte)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Isn't this where you start using a debugger?

    I mean, what are you going to do when the programs get 100 times larger, and it starts segfaulting?
    Learn how to use the debugger now, and you'll be well prepared for the larger programs to come.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by alphasil View Post
    Code:
        for(aux=initLista;aux!=NULL;aux=aux->seguinte)
        {
            printf("%s %s %s %d %s\n",aux->nodo.nomes,aux->nodo.curso,aux->nodo.inst,aux->nodo.nota, aux->nodo.curri);
            aux=aux->seguinte;
        }
    You wrote aux=aux->seguinte at the end of the loop body and in the for statement itself. If the first statement (in the loop body) hits the NULL node, the second statement (in the for statement) will dereference a NULL pointer.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    53
    Hi Christop

    Thank you, fixed now.

    If i want to print the front of the list, how should i do??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  2. Queue While Loop Help
    By bananasplit in forum C++ Programming
    Replies: 14
    Last Post: 04-03-2010, 06:19 PM
  3. infinit loop on istream test
    By Mario F. in forum C++ Programming
    Replies: 10
    Last Post: 06-10-2006, 01:08 PM
  4. linked-list queue
    By the_winky_files in forum C Programming
    Replies: 17
    Last Post: 11-21-2005, 03:57 PM
  5. want some help with link list and queue
    By farnush in forum C Programming
    Replies: 2
    Last Post: 02-28-2005, 03:34 PM