Recursive function to end a list

Asked

Viewed 1,440 times

1

I want to do a recursive function to offset the memory of each block in the list, but when I print it after using the function, it loops and memory addresses are printed when asked to print (the correct would be to print nothing).

Statement in the main: in the *list;

Call on main: finaliza_recursivo(&list);

in the *list is only a pointer to the first element (block) of the list.

Below two versions of the same function, the two are giving the same problem:

Main

int main()
{
    no *ini; //é um ponteiro para um bloquinho (inicio da lista)
    elem x;
    int erro;

    cria(&ini);

    x = 2;
    inserir(&ini, &x, &erro);
    x = 3;
    inserir(&ini, &x, &erro);
    x = 8;
    inserir(&ini, &x, &erro);

    imprimir(ini);

    finaliza_recursivo(&ini);

    return 0;
}

Function

void cria(no **inicio)
{
    *inicio = NULL;
}

void finaliza_recursivo(no **inicio)
{
    no *P;

    P = *inicio;

    if(P != NULL)
        finaliza_recursivo(&P->prox);
    free(P);
}

void finaliza_recursivo(no **inicio)
{
    if(*inicio != NULL)
        finaliza_recursivo(&(*inicio)->prox);
    free(*inicio);
}
  • 1

    Just to clarify? Are you printing after calling this function? In general it is recommended to use a Minimum and verifiable example

  • Your list is a correct chained list? If you are allocating the nodes using as pointer. Why are you using pointer pointer? If you did no *n = (no*) malloc(sizeof(no)) just call free(n).

  • I was just printing to see if I had the memory displaced. I have another non-recurring version that is working and after calling it and then printing, do not print anything. When I use recursive, when printing, it prints random addresses. @Kyllopardiun

  • What I pass there at the beginning is just a pointer to the first node of the list. I want to make him go through the list and move every node out when he gets to the stop condition. @Wakim

1 answer

0


After making free() the contents of the variable have an invalid value.

I advise you to replace this invalid value with NULL.

void finaliza_recursivo(no **inicio)
{
    if(*inicio != NULL)
        finaliza_recursivo(&(*inicio)->prox);
    free(*inicio);
    *inicio = NULL; /* limpa valor inválido */
}

I guess your problem isn’t with the role you say.

Here is a full version of the program:

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

struct no {
  int data;
  struct no *prox;
};

void finaliza_recursivo(struct no **inicio) {
  if (*inicio != NULL) finaliza_recursivo(&(*inicio)->prox);
  free(*inicio);
  *inicio = NULL;
}

void cria(struct no **inicio) {
  *inicio = NULL;
}

void inserir (struct no **inicio, int *valor, int *erro) {
  struct no *tmp;
  tmp = malloc(sizeof *tmp);
  *erro = 1; /* assume que vai haver erro */
  if (tmp != NULL) {
    *erro = 0; /* afinal nao houve erro */
    tmp->data = *valor;
    tmp->prox = *inicio;
    *inicio = tmp;
  }
}

void imprimir(struct no *inicio, const char *msg) {
  int empty = 1;
  if (msg) printf("%s:", msg);
  while (inicio) {
    printf(" %d", inicio->data);
    inicio = inicio->prox;
    empty = 0;
  }
  puts(empty ? " (lista vazia)" : "");
}

int main(void) {
    struct no *ini; /* e um ponteiro para um bloquinho (inicio da lista) */
    int x;
    int erro;

    cria(&ini);

    imprimir(ini, "recem criada");

    x = 2;
    inserir(&ini, &x, &erro);
    imprimir(ini, "depois de inserir 2");
    x = 3;
    inserir(&ini, &x, &erro);
    imprimir(ini, "depois de inserir 3");
    x = 8;
    inserir(&ini, &x, &erro);
    imprimir(ini, "depois de inserir 8");

    finaliza_recursivo(&ini);
    imprimir(ini, "depois de finaliza_recursivo");

    return 0;
}
  • I updated the code with this line, but the program hangs when I call this function. @pmg

  • @Giovani: see the full version, which works in ideone.

  • everything working, thank you.

Browser other questions tagged

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