0
#include <stdio.h>
#include <malloc.h>

struct el {
    int info;
    struct el* next;
};

struct el* create_el(struct el* Li)
{
    int num;

    printf("\n\nInsert number:\n\n");
    scanf("%d", &num);

    Li = (struct el*)malloc(sizeof(struct el));

    if (Li != NULL) {
        Li->info = num;
        Li->next = NULL;
    }
    return (Li);
}

struct el* push(struct el* L, struct el* e)
{ //inserts the elements from the head of the list
    if (L == NULL)
        return (e);
    else {
        e->next = L;
        L = e;
        return (L);
    }
}

void visualize(struct el* primo)
{
    printf("\n\nList-->");

    while (primo->next != NULL) {
        printf("%d", primo->info);
        printf("-->");
        primo = primo->next;
    }
    if (primo->next == NULL)
        printf("%d-->NULL", primo->info);
}

struct el* cancel(struct el** P, int val)
{ //delete element
    struct el* prec = NULL;
    struct el* curr = (*P);

    if (P == NULL) //case empty list
        return NULL;
    else if (prec == NULL) {
        if (curr->info == val) { //case 2 : if the element is the head
            (*P)->next = curr->next;
            free(curr);
            curr = NULL;
        }
    }
    else {
        while ((curr != NULL) && (curr->info != val)) {
            prec = curr;
            curr = curr->next;
        }
        if (curr->next == NULL && curr->info == val) { // case 3: the elemnt is the last one
            prec->next = NULL;
            free(curr);
            curr = NULL;
            return (prec);
        }
        else {
            if (curr->info == val) { //other cases
                prec->next = curr->next;
                free(curr);
                curr = NULL;
                return (prec);
            }
        }
    }
}

int main()
{
    struct el* head = NULL;
    struct el* element;
    struct el* list = NULL;

    int i, n;
    int elem;

    printf("Insert the number of elements for the list:\n\n");
    scanf("%d", &n);

    for (i = 0; i <= n; i++) {
        element = create_el(head);
        if (element != NULL) {
            list = push(list, element);
        }
    }

    visualize(list);

    printf("\n\nInsert the element that you want to cancel:");
    elem = scanf("%d", &elem);

    cancel(&list, elem);

    visualize(list);
}

All I've wanted to do was delete an element from a listr, but after all the procediment the list is printed without any modification.

Can anyone see whats wrong in the function cancel(which is meant to delete an element by including any possible position of it)?

mch
  • 9,424
  • 2
  • 28
  • 42
Alex_79
  • 11
  • 2

1 Answers1

0

In your function cancel, P is definitely not NULL (assuming OS has assigned it an address initially). prec is NULL the before execution enters if loop. So, execution executes the line

if(curr->info==val)

Now, if the value, val, you have provided doesn't match curr->info then execution exits the function without deleting any node.

awakened
  • 192
  • 9
  • 'else if(curr->info==val){ (*P)->next=curr->next; free(curr); curr=NULL; }' I've modified this part of the code so that prec doen't necessarly punt to NULL, but now, for every number I want to cancel, the code cancels just the element of the last node. I don't understand – Alex_79 Dec 12 '18 at 17:47