Skip to main content
edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

What's the cleanest way C function to implement afind and delete a node from a singly linked list in C?

fixed spelling, grammar, punctuation, spacing
Source Link
Snowbody
  • 8.7k
  • 25
  • 50

I'm looking for the most commonly used style for writing the delete()delete_item() function fromof a singysingly linked list, that find a matching item and deletes it. Is what I have the 'typical' or 'normal' solution? Are there more elegant ones?

What seems unelegantinelegant to me about my solution below, although I don't know a better way to express it, is that you needthe code needs to check the first record individually (i.e. a special case), then as you iterate you're really looking at iter->nextit goes through the iteration, it's not checking iter, it's checking iter->next, ahead of yourthe iterator's present location, because in a singly linked list you can't go backwards.

So, is there a cleaner way to write the delete_item()delete_item() function?

This is incidently a complete example that can be compiled and tested. The output:

I'm looking for the most commonly used style for writing the delete() function from a singy linked list. Is what I have the 'typical' or 'normal' solution? Are there more elegant ones?

What seems unelegant about my solution below, although I don't know a better way to express it, is that you need to check the first record individually, then as you iterate you're really looking at iter->next, ahead of your location, because in a singly linked list you can't go backwards.

So, is there a cleaner way to write the delete_item() function?

This is incidently a complete example that can be compiled and tested. The output:

I'm looking for the most commonly used style for writing the delete_item() function of a singly linked list, that find a matching item and deletes it. Is what I have the 'typical' or 'normal' solution? Are there more elegant ones?

What seems inelegant to me about my solution below, although I don't know a better way to express it, is that the code needs to check the first record individually (i.e. a special case), then as it goes through the iteration, it's not checking iter, it's checking iter->next, ahead of the iterator's present location, because in a singly linked list you can't go backwards.

So, is there a cleaner way to write the delete_item() function?

This is a complete example that can be compiled and tested. The output:

edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
Philluminati
  • 325
  • 1
  • 2
  • 7
Loading