Chained list without head, problem in intersection function

1

I am doing a series of functions with list headless, but a function called Intersection is not working correctly, the problem compiles without errors but this function specifically does not run and the program presents runtime error, in that function must pass as parameter two list and the function returns a third list with the intersection of the two lists. Here is the code below:

#include<stdio.h>
#include<stdlib.h>

struct reg {
     int conteudo;
     struct reg *prox;
};

typedef struct reg celula;

void insere (celula **p, int x) {
     celula *novo, *q;
     q = *p;
     novo = (celula *)malloc(sizeof(celula));
     novo->conteudo = x;

     if (*p == NULL) {
          novo->prox = NULL;
          *p = novo;
     }

     else {
           while (q->prox != NULL)
                  q = q->prox;

           novo->prox = q->prox;
           q->prox = novo;
     }
}

void imprima (celula *p) {
     celula *q;
     if (p == NULL) printf ("Nao");
     else {
          for (q = p; q != NULL; q = q->prox)
                printf ("%d ", q->conteudo);
         printf ("\n");
      }
}

int NumElementos (celula *p) {
     celula *q;
     int n = 0;

     for (q = p; q != NULL; q = q->prox)
         n++;

return n;
}

int Pertence (celula *p, int x) {
     celula *q; int pt;
     q = p;

     while (q != NULL && q->conteudo != x)
           q = q->prox;

     if (q->conteudo == x) pt = 1;
     else pt = 0;

return pt;
}

celula *Interseccao (celula *p, celula *q) {
     celula *aq, *ap, *r = NULL;
     int aux;

     for (ap = p; ap !=NULL; ap = ap->prox) {
           for (aq = q; aq != NULL; aq = aq->prox) {
                aux = aq->conteudo;
                   if (Pertence(ap, aux) ==  1) {
                          if (Pertence(r, aux) == 0)
                                insere(&r, aux);
                 }
           }
      }
    return r;
}

int main () {
     celula *lista = NULL;
     celula *lst = NULL;
     celula *lis = NULL;

     int ne, p;

     insere (&lista, 8);
     insere (&lista, 2);
     insere (&lista, 1);
     insere (&lista, 3);
     insere (&lista, 9);
     imprima(lista);
     ne = NumElementos (lista);
     printf ("%d ", ne);
     p = Pertence(lista, 3);
     if (p == 0) printf("\n pertence");
     else printf("\npertence\n");
     insere (&lst, 7);
     insere (&lst, 3);
     insere (&lst, 5);
     insere (&lst, 1);
     imprima(lst);
     lis = Interseccao(lista, lst);
     imprima(lis);

return 0;
}
    
asked by anonymous 26.10.2016 / 17:08

1 answer

2

The Pertence() function is not checking whether the p (assigned to the local variable q ) is a null pointer, and when accessing the conteudo element, the runtime < in>.

Just change the if within the function to do this check before access:

int Pertence (celula *p, int x) {
     celula *q; int pt;
     q = p;

     while (q != NULL && q->conteudo != x)
           q = q->prox;

     // AQUI  => verifica se q é diferente de NULL antes de acessar conteudo 
     if (q != NULL && q->conteudo == x) pt = 1;
     else pt = 0;

return pt;
}

The result of the execution, after the change:

8 2 1 3 9
5
pertence
7 3 5 1
3 1       <== interseccão
    
26.10.2016 / 18:37