### CS171, Homework 7

• Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs171/hw7 cp /home/cs171000/Handouts/hw7/*.java ~/cs171/hw7 ```

• Overview

• This homework consists of 2 parts:

• Part 1:

 Write the void put(x, k) method that inserts a node containing the value x at the position k of the list

• Part 2:

 Write the double remove(k) method that removes the node at the position k of the list The double remove(k) method must return the value in the deleted node

• Notation: the node at position k

• Suppose the list is as follows:

We will call:

 The node with value 9, the node at position 1         The node with value 7, the node at position 2 The node with value 4, the node at position 3 The node with value 6, the node at position 4

• Part 1 of Assignment

• Assignment:

• Edit the file List.java and write the void put(x, k) method that inserts a node containing the value x at the position k of the list

• Test program file used in part 1:

 testProg1.java

• Compile and run:

 ``` javac testProg1.java java testProg1 ```

Correct result is:

 ``` Testing the put() method Insert 1 is correct ! myList = [7.0] Insert 2 is correct ! myList = [6.0 , 7.0] Insert 3 is correct ! myList = [1.0 , 6.0 , 7.0] Insert 4 is correct ! myList = [1.0 , 2.0 , 6.0 , 7.0] Insert 5 is correct ! myList = [1.0 , 2.0 , 3.0 , 6.0 , 7.0] Insert 6 is correct ! myList = [1.0 , 2.0 , 3.0 , 4.0 , 6.0 , 7.0] Insert 7 is correct ! myList = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0] Insert 8 is correct ! myList = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0] Insert 9 is correct ! myList = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0] ```

• What must the put(x,k) algorithm do:

 The put(x,k) can insert a node at any position in the list The value k will determine the insert position

Examples:

• If the list is like this:

Then put(999, 1) will insert a node with valye 999 at position 1.

The list will be updated to:

• If the list is like this:

Then put(999, 3) will insert a node with valye 999 at position 3.

The list will be updated to:

• If the list is like this:

Then put(999, 5) will insert a node with valye 999 at position 5.

The list will be updated to:

• Hint:

• You must use a loop to find these 2 nodes: the node at position k−1 and the node at position k

• Use 2 pointer variables (ptr1 and ptr2) and adapt the algorithm for finding the one-before-the-deletion node on this webpage:

to find the node at position k−1 and the node at position k.

• After you have found these 2 nodes, you must link a new node with the value x between the node at ptr1 and the node at ptr2:

• Warning:

• There is one special case that you must take care of:

 Inserting the node at head (start) of the list (How can you detect this case ???) We have learned how to insert a node at the head of a list on this webpage: click here

• Part 2 of Assignment

• Assignment:

• Edit the file List.java and write the double remove(k) method that removes the node at the position k of the list

The remove(k) method must return the value that was stored in the deleted node

• Test program file used in part 2:

 testProg2.java

• Compile and run:

 ``` javac testProg2.java java testProg2 ```

Correct result is:

 ``` Delete 1 is correct ! x = 1.0 *** myList = [2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0] Delete 2 is correct ! x = 5.0 *** myList = [2.0 , 3.0 , 4.0 , 6.0 , 7.0 , 8.0 , 9.0] Delete 3 is correct ! x = 6.0 *** myList = [2.0 , 3.0 , 4.0 , 7.0 , 8.0 , 9.0] Delete 4 is correct ! x = 9.0 *** myList = [2.0 , 3.0 , 4.0 , 7.0 , 8.0] Delete 5 is correct ! x = 8.0 *** myList = [2.0 , 3.0 , 4.0 , 7.0] Delete 6 is correct ! x = 3.0 *** myList = [2.0 , 4.0 , 7.0] Delete 7 is correct ! x = 2.0 *** myList = [4.0 , 7.0] Delete 8 is correct ! x = 7.0 *** myList = [4.0] Delete 9 is correct ! x = 4.0 *** myList = [] ```

• What must the remove(k) algorithm do:

 The remove(k) can delete a node at any position in the list The value k will determine the delete position

Examples:

• If the list is like this:

Then remove(1) will delete the node at position 1.

The list will be updated to:

• If the list is like this:

Then remove(3) will delete the node at position 3.

The list will be updated to:

• If the list is like this:

Then remove(4) will delete the node at position 4.

The list will be updated to:

• Hint:

• You must use the same algorithm (discussed above) to find these 2 nodes: the node at position k−1 and the node at position k

• After you have found these 2 nodes, you can delete the node at position k by changing this link:

• Warning:

• There is one special case that you must take care of:

 Deleting the node at head (start) of the list (How can you detect this case ???) We have learned how to delete a node at the head of a list on this webpage: click here

• Turnin

• Use the following command (from inside your cs171/hw7 project directory) to turn in your program:

 ``` cd ~/cs171/hw7 /home/cs171000/turnin List.java hw7 ```