Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: remove recursively elements of a list

  1. Jul 14, 2016 #1
    Hello.

    It's given the following type (sorry for the mix italian-english):

    Code (Text):

    struct El{
        int info;
        struct El* next;
    };

    typedef struct El ElementoLista;
    typedef ElementoLista* ListaDiInteri;
     
    And I have to remove all elements of value n; I did this:


    Code (Text):

    void remLista(ListaDiInteri *l, int n)
    {
        if((*l)->next==NULL) {(*l)=NULL; return;}
        if((*l)->info==n) {*l=(*l)->next; remLista(&((*l)->next),n);}
        else remLista(&((*l)->next),n);

    }
     
    I dislike the use of those pointers. Is there any other way or something?

    Thank you.


    This is the full code if you want to try:

    Code (Text):

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


    struct El {
        int info;
        struct El *next;
    };

    typedef struct El ElementoLista;
    typedef ElementoLista* ListaDiInteri;

    void pushBack(ListaDiInteri *l, int n);
    void printLista(ListaDiInteri l);
    void remLista(ListaDiInteri *l, int n);



    int main()
    {

        ListaDiInteri l=NULL;

        pushBack(&l,1);
        pushBack(&l,2);
        pushBack(&l,4);
        pushBack(&l,6);
        pushBack(&l,1);
        pushBack(&l,4);
        pushBack(&l,5);
        pushBack(&l,1);
        pushBack(&l,3);
        pushBack(&l,1);





        printLista(l);

        remLista(&l,1);

        printLista(l);



        return 0;
    }


    void pushBack(ListaDiInteri *l,int n)
    {

        if(*l==NULL){
            *l=malloc(sizeof(ElementoLista));
            (*l)->info=n;
            (*l)->next=NULL;
            return;
        }

        if((*l)->next==NULL){
            ElementoLista *e=malloc(sizeof(ElementoLista));
            e->info=n;
            e->next=NULL;
            (*l)->next=e;
        }
        else pushBack(&((*l)->next),n);
    }

    void printLista(ListaDiInteri l)
    {

        if(l==NULL) printf("Lista vuota\n");

        if(l->next==NULL){
            printf("%d\n",l->info);
            return;
        }
        else{
            printf("%d ",l->info);
            printLista(l->next);
        }

    }


    void remLista(ListaDiInteri *l, int n)
    {
        if((*l)->next==NULL) {(*l)=NULL; return;}
        if((*l)->info==n) {*l=(*l)->next; remLista(&((*l)->next),n);}
        else remLista(&((*l)->next),n);

    }

     

    As you can see I have the same problem with pushBack().
     
  2. jcsd
  3. Jul 14, 2016 #2
  4. Jul 14, 2016 #3
    I know about the missing free; I need to learn this in C for the exam.
     
  5. Jul 15, 2016 #4
    Is remLista for removal?
    Then, look at your pushBack. When do you use malloc?
    Free
    is the symmetric operation: you are to deallocate your node.

    Here you can find the thorough instructions both for recursive and iterative implementation: https://www.cs.bu.edu/teaching/c/linked-list/delete/
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C: remove recursively elements of a list
  1. Recursion in C (Replies: 12)

Loading...