Answer to Question #263590 in C++ for Saitama

Question #263590

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)


1
Expert's answer
2021-11-09T23:39:20-0500

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;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS