Program stopping responding in C - chained list

Asked

Viewed 169 times

0

I’m doing a college job in C and I’ve pretty much finished it. However, I got caught up in a bug that I can’t get out of. My show, when I cross the line aux1=aux1->next; simply stops working. It stops responding just like it does when you give a scanf unused & before the variable. Can anyone answer me why? I think it’s basic detail.

Follow the code below:

int addin (lista *no1, lista *no2, lista *no3) {
long int soma;
soma=0;
nodeptr *aux1;
nodeptr *aux2;
aux1=no1->inicio->next;
aux2=no2->inicio->next;

while (aux1 != NULL || aux2 != NULL) {
    soma=soma+(aux1->info+aux2->info);
    printf ("Voltei aqui");
    printf ("\nValor do aux1->info: %d\n", aux1->info);
    printf ("\nValor do aux2->info: %d\n", aux2->info);
    printf ("\nValor da soma: %d\n", soma);
    insereElemento(no3, (soma%100000));
    if (soma/100000!=0) {
        soma=soma/100000;
    }
    aux1=aux1->next;

    printf ("Valor novo do aux1: %d", aux1->info);
    aux2=aux2->next;
    printf ("Valor novo do aux2: %d", aux2->info);
}

The statement of the list nodes is given as follows:

typedef struct node {
    long int info;
    struct node *next;
} nodeptr;

typedef struct {
    nodeptr *inicio;
    nodeptr *fim;
} lista;

EDIT.: Element insertion function in the list

int insereElemento (lista *no, int elemento) {
nodeptr *novo = (nodeptr *) malloc (sizeof(nodeptr));
novo->info=elemento;

if (no->inicio==NULL) {
    inicializaLista(no);
} else {
    no->fim->next=novo;
    no->fim=novo;
    novo->next=NULL;
    printf ("\nElemento inserido com sucesso na lista: %d\n", no->fim->info);
}

}
  • I took a cursory look, and the "while" condition is reversed, I should use "&&" instead of "||" as it is now.

1 answer

0

You would have to see how the list was started, the problem may be in the initialization of the values. And another thing, at the beginning of the code the aux’s shouldn’t just point to the beginning?

Before:

aux1=no1->inicio->next;
aux2=no2->inicio->next;

Changeover:

aux1=no1->inicio;
aux2=no2->inicio;
  • No no. The job asks to start from the second even, the first is -1. I will edit to put the insertion function in the post.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.