Chained list without head, intersection function problem

Asked

Viewed 395 times

1

I’m doing a series of function with chained list without head, but a function called Intersection is not working properly, the problem compiles without errors but this function specifically does not run and the program shows error in this function must pass as parameter two list and the function returns me a third list with the intersection of the two lists. Follow 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;
}

1 answer

2


The function Pertence() is not checking if the parameter p (assigned to the local variable q) is a null pointer and when accessing the element conteudo, error occurs in the Runtime.

Just change the if within the function to make 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 implementation, after the change:

8 2 1 3 9
5
pertence
7 3 5 1
3 1       <== interseccão

Browser other questions tagged

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