- 181
- 4
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If, but if you want to remove the "break" then it will remove all itemsentries that match, you have to use continue
instead of break
.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If you want to remove all items that match, you have to use continue
instead of break
.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds, but if you remove the "break" then it will remove all entries that match.
- 181
- 4
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If you want to remove all items that match, you have to use continue
instead of break
and move the current = &(*current)->next
out of the for
clause and to the end of the loop since it must not be executed when just having deleted an element.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If you want to remove all items that match, you have to use continue
instead of break
and move the current = &(*current)->next
out of the for
clause and to the end of the loop since it must not be executed when just having deleted an element.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If you want to remove all items that match, you have to use continue
instead of break
.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds, but if. If you remove the "break" then it willwant to remove all entriesitems that match, you have to use continue
instead of break
and move the current = &(*current)->next
out of the for
clause and to the end of the loop since it must not be executed when just having deleted an element.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds, but if you remove the "break" then it will remove all entries that match.
The neatest way to deal with this is to use what I was taught as the "2 pointer trick" (thank you Charles Lindsay all those years ago):
Instead of holding a pointer to the record you hold a pointer to the pointer that points to it - that is you use a double indirection. This enables you to both modify the pointer to the record and to modify the record without keeping track of the previous node.
One of the nice things about this approach is that you don't need to special case dealing with the first node.
An untested sketch using this idea for delete_item (in C++) looks like:
void delete_item(node** head, int i) {
for (node** current = head; *current; current = &(*current)->next) {
if ((*current)->x == i) {
node* next = (*current)->next;
delete *current;
*current = next;
break;
}
}
}
This loop will break after the first entry it finds. If you want to remove all items that match, you have to use continue
instead of break
and move the current = &(*current)->next
out of the for
clause and to the end of the loop since it must not be executed when just having deleted an element.