1

The book I study #include an exercise that asks us for modifying the following code which deletes a node from a linked list and building another function that does the exact thing with only one pointer:

struct node {
    int value;
    struct node * next;
};

struct node delete_from_list( struct node * list , int n){
struct node *cur, *prev;

for (cur = list, prev = NULL;
    cur != NULL && cur -> value != n;
    prev = cur, cur -> next)
;

if(cur == NULL)
    return list;
else if(prev == NULL)
    list = list -> next;
else
prev -> next = cur -> next;

free(cur);
return list;
}

Some code I've seen online is:

struct node *delete_from_list(struct node **list, int n) {
    struct node *entry = *list;
    while (entry) {
        if (entry->value == n) {
            *list = entry->next;
            free(entry);
            break;
        }
        list = &entry->next;
        entry = entry->next;
    }
    return *list;
}

But i have two objections for this:

  1. This code contains two pointers actually entry and list.

  2. We are free()ing the entry yet proceed to use it which "feels" like an error.

Please, if you are willing to help, either explain my objections or write a new code. Thanks.

5
  • 1
    I'd start with a piece of paper and a pencil and draw the situation. Commented Mar 24, 2020 at 13:44
  • ad.2: you are not using the entry once freed. There is a break; Commented Mar 24, 2020 at 13:49
  • Yes, i drew it but it is actually what made me question it. I drew entry pointing to a block of two boxes which means a struct node structure. And using free(entry) was just replacing the block of two boxes with a huge cross. It is right, isn't it? Commented Mar 24, 2020 at 13:51
  • @PaulOgilvie Oh OK. That makes more sense right now. Thanks but isn't the code still has 2 pointers? Commented Mar 24, 2020 at 13:53
  • 1
    Welcome to stackoverflow! Its a nice little exercise. I know what you mean about the two pointers, but I think that the question setter is excluding the parameter. If the parameter is included then the original code has three: list, cur, prev. Commented Mar 24, 2020 at 14:56

1 Answer 1

1
  1. This code contains two pointers actually entry and list.

You are required to build an algorithm that uses a single pointer. In your case, though it seems like two, entry is that one pointer which the algorithm uses apart from list which is actually an input to the function containing the algorithm.

  1. We are free()ing the entry yet proceed to use it which "feels" like an error.

The code doesn't use entry after freeing it. Once freed, the loop breaks immediately due to the break statement succeeding the call to free.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.