3 ms·
You can get the same effect by using a “dummy” node. The dummy node has a next pointer, but no data, and the dummy node always exists at the front of the list.
by DerekL 8y ago
You can get the same effect by using a “dummy” node. The dummy node has a next pointer, but no data, and the dummy node always exists at the front of the list. The list structure contains the dummy, instead of using a pointer to the front of the list.
struct Node
{
struct Node* next;
};
struct DataNode
{
struct Node* next;
struct Value value;
};
struct List
{
struct Node dummy;
// You can also add length, etc.
};
// entry must be in list
void remove_list_entry(struct List* list, struct Node* entry)
{
Node *prev = &list->dummy;
while (prev->next != entry) {
prev = prev->next;
assert(prev != NULL);
}
prev->next = entry->next;
free(entry);
}