C211 I211 Problem Solving and Programming II

C211 I211 Lab 6

Due Date: Monday, October 2, 2023.

Ex. 1. Linked List

In this lab, we will start a project implementing and testing simple linked lists in Java.

a. Classes

Create a project called lab6 and a package inside it called lab6. Add a class called Node to package implementing a node of a linked list containing an integer value and a reference next to another node. Write a default constructor and a constructor with an integer value in this class.

Add a class called List to the project containing a reference to the first node in the list called head and a default constructor, like in the presentation slides.

Add a third class to the project called TestList containing the main method.

b. Insertion and Remove.   Add the methods insertFront, removeFront(), and removeSecond discussed in class to the list class. Add some tests for them in the class TestList.

Add another insertion function to the class, insertOrder taking an integer value as parameter, that inserts the value into the list assuming that it is kept in ascending order. The method must first find a good place to insert the new node, then insert it there.

c. Traversal.   Add the methods printNodes, countNodes, and lastNode discussed in the lecture.

d. Interface.   In the class TestList, in the main, add an interface loop that continues while a boolean variable quit is false. Declare that variable in the main and initialize it as false. Declare an integer variable choice and a List variable, initialized with an empty list.

Then in the loop, print a number of options for the user to choose from with operations to be done on the list: 0 to quit, 1 for insert, 2 for insert in order, 3 for delete the first, and so on, for each of the functions we may want to test. Ask the user to enter the choice, then input it as an integer. Then based on the input form the user, perform each of those actions. Note that for some of them, you may have to input something additional from the user.

Use a switch statement to test the differen possible answers from the user, and create a separate method for printing the options and for processing the answer, to avoid over-cluttering the main method itself.

Test the program to make sure that all the functionality works.

Lab Submission

The homework will continue the same project, so you may want to wait until you also complete it before uploading the files. All .java files created for this project should be uploaded to Canvas in Assignments - Homework 6.