C: remove recursively elements of a list

In summary, the conversation discusses a code for removing all elements with a specific value from a linked list. The code is written in C and uses pointers. The speaker expresses dislike for using pointers and asks if there is a different way to implement the code. The conversation also addresses the issue of not freeing memory and suggests using C++ with the built-in list container as an alternative. The speaker also mentions the need to learn about memory management for an upcoming exam and provides a resource for implementing the code in both a recursive and iterative manner.
  • #1
RaamGeneral
50
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
  • #2
  • #3
I know about the missing free; I need to learn this in C for the exam.
 
  • #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/
 

1. How can I remove elements from a list in C recursively?

To remove elements from a list in C recursively, you can use a function that takes in the list and the element to be removed as parameters. Inside the function, you can use a loop to iterate through the list and check each element against the one to be removed. If a match is found, the element can be removed using the appropriate list manipulation techniques. The function can then be called recursively until all instances of the element have been removed.

2. Is it possible to remove elements from a nested list in C recursively?

Yes, it is possible to remove elements from a nested list in C recursively. You can use a function that takes in the nested list and the element to be removed as parameters. Inside the function, you can use nested loops to iterate through the list and all its sublists, and remove the element from each sublist. The function can be called recursively until all instances of the element have been removed from the nested list.

3. How do I avoid an infinite loop when removing elements from a list recursively in C?

To avoid an infinite loop when removing elements from a list recursively in C, you can use a base case in your recursive function. This base case should check for the condition where the list is empty, and if so, return the list as is without making any further changes. This will ensure that the recursive function terminates when all elements have been removed from the list.

4. Can I use a recursive approach to remove multiple elements from a list in C?

Yes, you can use a recursive approach to remove multiple elements from a list in C. You can modify your recursive function to take in an array of elements to be removed as a parameter, and then use nested loops to check for each element in the list and remove it if found. This modified function can be called recursively until all elements in the array have been removed from the list.

5. How does removing elements recursively affect the time and space complexity of my code in C?

Removing elements recursively in C can potentially increase the time and space complexity of your code. This is because recursive functions often require more memory to store the function call stack, and the time complexity can also increase as the function is called multiple times until all elements are removed. However, the exact impact on time and space complexity will depend on the specific implementation of your recursive function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
Back
Top