0

I have a problem with my code. I want to delete an element from a circular list and I did it but my problem is that instead of not seeing that number anymore I see a 0(zero) in the same position of the number that I deleted. Can anyone help me, please? The problem is in the DeleteElement

    #include<stdio.h>
    #include<stdlib.h>
    struct nodo{
        int info;
        struct nodo *next;
    };
    typedef struct nodo* tlista;
    int PrintList(tlista l){
      if(l){
        tlista pc=l;
        do{
          printf("%d",l->info);
          l=l->next;
        }while(l!=pc);
      }
    }
    void DeleteElement(tlista l,int elem){
        tlista pc=l;
        do{
          if(l->info==elem){
            tlista k=l;
            l=l->next;

            free(k);
          }else{
            l=l->next;
          }

        }while(pc!=l);
    }
    int CreateList(tlista *l,int n){
        tlista new=(tlista)malloc(sizeof(struct nodo));
        if(new){
            new->info=n;
        if((*l)==NULL){
            *l=new;
            new->next=new;

        }
        else{
           new->next=(*l)->next;
          (*l)->next=new;
        }return 1;
        }else{return 0;}
    }
    int main(){
        tlista l=NULL; int number;
        int NumberInsideTheList; int numbertofind;
        int thenumbertodelete; int i=0;
        int risult;
        printf("How many numbers do you want to insert  = ");
        scanf("%d",&number);
        while(i<number){
            printf("Insert a number that you want to insert into the 
        list \n");
            scanf("%d",&NumberInsideTheList);
            CreateList(&l,NumberInsideTheList);
            i++;
        }
        printf("\n\n");

          printf("number to delete = ");
          scanf("%d",&thenumbertodelete);
          DeleteElement(l,thenumbertodelete);
          printf("\n\n");
          PrintList(l);
        return 0;
    }

Thank you.

Alex97
  • 401
  • 1
  • 8
  • 21
  • Possible duplicate of [Removing elements from an array in C](https://stackoverflow.com/questions/15821123/removing-elements-from-an-array-in-c) – Hack5 May 21 '18 at 15:56

1 Answers1

0

Your solution has not been adequately analyzed. There are a few points to note,

1 - A circular list is a simple list where the last element points to the first element. The means in your list it is important to keep track of the first and last node since deleting items in this position would be handled diferently.

2 - If you list contains more than one node with the same value, would you eliminate the first one you see or every instance of the given number.

3 - To delete a node, you need a reference to the node before it. You point the *next reference of the previous node to the *next reference of the node you want to delete.

4 - Rather than using the function free(k) I recommend k=null

Review your solution and if you have further questions, I'ld be glad to help.

Koji D'infinte
  • 1,309
  • 12
  • 20
  • Not calling `free()` on the removed list node means that the code leaks memory – Chris Turner May 21 '18 at 16:25
  • For beginners, handling memory and exception is always a challenge. Based on my personal experience and since this is not productive application, I recommend freeing resources at the end of the program, to keep things simple. Once the fundamentals is mastered, optimization can be learned. – Koji D'infinte May 21 '18 at 16:34
  • freeing memory isn't an "optimization", it's a fundamental part of working with allocated memory and best learnt from the outset so that it becomes second nature rather than an after thought – Chris Turner May 21 '18 at 16:39