C211 I211 Problem Solving and Programming II

C211 I211 Homework 6

Due Date: Monday, October 2, 2023.

Please complete Lab 6 before completing the homework.

Ex. 1. Linked Lists

In this homework, you will continue working on the project started in the lab and add more functionality to it.

a. Length attribute.   Add a public integer attribute in the class List called length that keeps track of how many elements we have in the list. Modify all the existing methods in this class to make sure that the length is updated properly. For example, in the functions inserting new elements in the list, you should increment the length, and in those removing an element you should decrease it. Modify the function printNodes to add a statement at the beginning telling us how many elements we have in the list based on the length. In all the other methods you implement for the homework, also make sure to update the length properly.

b. Increment values.   Add a method in the class List with the header (signature)

public void incrementValues() that traverses the list and increments the value of every node by 1. This function should not output anything and should not insert or remove nodes from the list. Add a test case for it in the main.

c. Search by value.   Add a method with the header

public Node searchValue(int val)

to the class List. This method should traverse the list and search the nodes for the value passed in as parameter. If the value is found in the list, the method should return a reference to the node containing it. If the value is not in the list, or if the list is empty, the method should return null. The method should not output anything to the console itself.

Add a test case in the main in the class TestList for this function. Test if the value returned by the method is null, and if it is, output a message saying that the value was not found. If the returned reference is not null, output a message saying that the value is in the list.

d. Remove by value.   Add a method with the header

public boolean removeValue(int val)

to the List class. This method should look through the list for the first node containing the given value. If such a node is found, the method should remove it and return true. If the value is not found, the method should return false. If the list contains multiple occurrences of the given value, only the first one should be removed.

Add a case in the testing interface for this function and test it to make sure it works.

Homework Submission

Upload all the files .java created in this project to Canvas - Assignments - Homework 6.