1. Requesting clarification regarding double pointers and linked lists

[FONT=sans-serif]Hi everyone, I am currently learning C using KNKing's book. Currently I'm confused by pointers to pointers/linked lists and would like to kindly request for some help and clarifications, with regards to this function below. Thank you all and bless.

The below function will be called with (&first,10), with first being a pointer to the first node in the list and 10 being the value in which the node is to be deleted.
Code:
```struct node *delete_from_list(struct node **list, int n)
{
struct node *item = *list;
while (item) {
if (item->value == n) {
*list = item->next;
free(item);
break;
}
list = &item->next;
item = item->next;
}
return *list;
}```

my question is. whenver i use the "*list",
*list = item->next;
am i not editing the address which the pointer "first" contains? so when the above statement is executed wont "first" no longer be pointing to the first node? Thank you!

1 -> 2 -> 3 -> 4 -> 5
and you try to delete 3 by doing this
*list = item->next;

You end up with 4 -> 5 as your list.

Typically, you need to keep track of the previous node as well.
Code:
```item = head;
prev = NULL;
while (item) {
prev = item;
item = item->next;
}```
So that when you reach the node to delete, say 3, you have (or can easily obtain) pointers to nodes 2, 3, and 4.

Because you want to turn
1 -> 2 -> 3 -> 4 -> 5
into
1 -> 2 -> 4 -> 5

3. Yes

Originally Posted by Salem
1 -> 2 -> 3 -> 4 -> 5
and you try to delete 3 by doing this
*list = item->next;

You end up with 4 -> 5 as your list.

Typically, you need to keep track of the previous node as well.
Code:
```item = head;
prev = NULL;
while (item) {
prev = item;
item = item->next;
}```
So that when you reach the node to delete, say 3, you have (or can easily obtain) pointers to nodes 2, 3, and 4.

Because you want to turn
1 -> 2 -> 3 -> 4 -> 5
into
1 -> 2 -> 4 -> 5
Yes sir I get what you are saying, normally theres 2 pointers. But this is an exercise in a textbook (c programming a modern approach) which asks that
Modify the delete_from_list function so that is uses only one pointer variable instead of two (cur and prev).

which is why I was wondering. If I use the code as posted in my original post, "first" will no longer be pointing to the first node, so the code aint really right, right? Thank you for the fast response!

4. > Modify the delete_from_list function so that is uses only one pointer variable instead of two (cur and prev).
I'm always suspicious of books / tutors that try to teach magic tricks as opposed to sound programming.

But FWIW, try using recursion to get an extra variable 'for free'.