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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top