Define a function that will be given a pointer pointing to the first element of the linked list and two strings named first_string and second_string respectively. The function must search for the first_string in the list. If the list does not contain such a string, the function leaves the list unchanged. Otherwise, the function must add a new element in the linked list with the value of second_string after the first occurrence of the element containing the first_string. The function prototype should look like void add_after(node* , string , string)
void add_after(node* nd, string first_string, string second_string)
{
node temp = *nd;
while (temp.next != nullptr)
{
if (temp.str != first_string)
{
temp = *temp.next;
}
else
{
node add;
add.str = second_string;
add.next = temp.next;
add.prev = &temp;
temp.next->prev = &add;
temp.next = &add;
temp = *add.next;
}
}
nd = &temp;
}
Comments
Leave a comment