For example, if your linked list was 1 -> 2 -> 3 -> 4, and you think there is an error with this code, you could say something like “the problem with the code is xxx and after calling update(3, 5), the linked list will look like (and then show your linked list)”   If you think the code does not have an error, still provide an example of calling update with your own values and show what the linked list ends up looking like

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 3SA
icon
Related questions
Question

Given the following function to update a node in a standard linked list:

public boolean update(Node head, int oldValue, int newValue)

{

      if (head == null)

            return false;

      while (head != null)

            if (head.data == oldValue)

            {

                  head.data = newValue;

                  return true;

            }

            else

                  head = head.next;

      return false;

}

 

There may be an error with the code. Answer the following:

 

  • Design your own linked list of 4 nodes with different values
  • If the code has an error, describe the issue and provide an example of calling the update function with values of your choice and show what the linked list looks like
    1. Also, show the code changes you would make to fix the error
  • If the code does not have an error, provide an example of calling the update function with values of your choice and show what the linked list looks like

 

For example, if your linked list was 1 -> 2 -> 3 -> 4, and you think there is an error with this code, you could say something like “the problem with the code is xxx and after calling update(3, 5), the linked list will look like (and then show your linked list)”

 

If you think the code does not have an error, still provide an example of calling update with your own values and show what the linked list ends up looking like

 

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning