vault backup: 2024-11-19 22:05:20

This commit is contained in:
zleyyij 2024-11-19 22:05:20 -07:00
parent a406845641
commit 56b77cc8ff

View File

@ -72,11 +72,14 @@ struct node *insert_into_ordered_list(struct node *list, struct node *new_node)
} }
``` ```
In the above code, if the new item needs to be ine In the above code, if the new item needs to be inserted at the *end* of the list, it breaks, because `cur` is set to `NULL`, then it attempts to access `cur->value`.
```c ```c
struct node *insert_into_ordered_list(struct node *list, struct node *new_node) { struct node *insert_into_ordered_list(struct node *list, struct node *new_node) {
struct node *cur = list, *prev = NULL; struct node *cur = list, *prev = NULL;
while (cur->value <= new_node->value) { while (cur->value <= new_node->value) {
if (cur->next == NULL) {
break;
}
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
} }