C: remove recursively elements of a list

  • Context:
  • Thread starter Thread starter RaamGeneral
  • Start date Start date
  • Tags Tags
    Elements List
Click For Summary

Discussion Overview

The discussion revolves around a C programming task involving the removal of elements from a linked list based on a specified value. Participants explore the implementation details, memory management, and alternative approaches to handling pointers in C.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a recursive function, remLista, to remove elements of a specified value from a linked list but expresses discomfort with the use of pointers.
  • Another participant suggests using C++ and its built-in list container to avoid pointer complexity, indicating that the original code lacks memory deallocation with free.
  • A third participant acknowledges the memory management issue but emphasizes the need to learn proper memory handling in C for an upcoming exam.
  • Another participant questions the purpose of remLista and points out the importance of using malloc correctly in the pushBack function, highlighting the necessity of deallocating nodes.
  • A link to external resources is provided for further guidance on both recursive and iterative implementations of linked list deletion.

Areas of Agreement / Disagreement

Participants express differing views on the use of pointers in C, with some advocating for alternative languages or methods, while others focus on improving the current C implementation. There is no consensus on the best approach to handle the linked list operations or memory management.

Contextual Notes

Participants note the absence of memory deallocation in the provided code, which may lead to memory leaks. There are also unresolved questions about the implementation details and the effectiveness of the recursive approach.

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/
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K