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/
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top