I was watching a youtube video on doubly linked lists in C. One of the functions made for the doubly linked list was an "insert_after_node" function, which as the name suggests inserts a new node after an existing one.
For context node_t is a typedef for this struct:
struct node {
int value;
struct node* next;
struct node* prev;
};
Here is the function:
void insert_after_node(node_t *node_to_insert_after, node_t* newnode) {
newnode->next = node_to_insert_after->next;
if (newnode->next != NULL) {
newnode->next->prev = node_to_insert_after;
}
newnode->prev = node_to_insert_after;
node_to_insert_after->next = newnode;
}
Logically Im thinking that the newnode->next->prev = node_to_insert_after; line of the function is incorrect and should instead be newnode->next->prev = newnode;. I mean isnt the previous node of the new nodes next node just the new node? No one seems to have commented on this which makes me think I might be wrong.
Am I wrong and if so why is the initial function correct?
