C: remove recursively elements of a list

  • Thread starter Thread starter RaamGeneral
  • Start date Start date
  • Tags Tags
    Elements List
AI Thread Summary
The discussion revolves around a C implementation of a linked list and the challenges faced in removing elements with a specific value. The user has provided a code snippet that includes a structure definition for a linked list, along with functions to add elements, print the list, and remove elements. The main concern expressed is the complexity of using multiple pointers in the `remLista` function, leading to a desire for a simpler approach. Additionally, there is a noted absence of memory management, specifically the lack of `free` calls to deallocate memory for removed nodes, which is crucial for preventing memory leaks. The user is encouraged to explore both recursive and iterative methods for list manipulation and is directed to resources for further learning about linked list operations in C. The suggestion to consider C++ for its built-in list container is also mentioned as an alternative to reduce pointer complexity.
RaamGeneral
Messages
49
Reaction score
1
Hello.

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

Code:
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:
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:
#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().
 
Technology news on Phys.org
I know about the missing free; I need to learn this in C for the exam.
 
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/
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top