Programming Assignment 3
CS 27500
Data Structures
Fall, 2021

This assignment is due Friday, December 10.

This assignment makes use of the files contained in this zip file. This assignment is about linked list operations and it is based on the examples we did in class from LinkedList_DS.zip.

In the zip file there is a file LinkedListMethods.java that you need to complete. When you have completed that file, the programs TestLinkedListMethods_v1.java and TestLinkedListMethods_v2.java should compile and run. When they run, they should produce output that looks exactly like the contents of TestLinkedListMethods_v1_output.txt and TestLinkedListMethods_v2_output.txt.

There are six methods that you need to write in LinkedListMethods.java. Here are the six declarations.

   public static     Node<Integer> makeListOfInt(int size, int start)
   public static     int           sum(Node<Integer> head)
   public static <E> Node<E>       select(Node<E> head, int index)
   public static <E> E             deleteAfter(Node<E> selection)
   public static <E> void          insertAfter(Node<E> selection, E item)
   public static <E> void          append(Node<E> list1, Node<E> list2)

Notice that the last four method are "generic methods". They work on any type of linked list. The first two methods are specific to linked lists with integer nodes.

The method makeListOfInt() builds a linked list with integer nodes and then it returns a reference to the first node (the head) of the list it built. The size parameter determines the length of the new list. The data in the first node of the new list is given by start and then the data in all the rest of the nodes increases by 1 as you move down the list. So, for example, the method call

      LinkedListMethods.makeListOfInt(5, 12);

should return a reference to this list.

      [12, 13, 14, 15, 16]

The sum() method only works for linked lists with integer nodes and it sums up the values from all the nodes of the given list.

The select() method takes a reference to a linked list and an integer index. This method should return a reference to the node in the list that has that index (where the first node in the list has index zero). You can assume that the list has a node with that index (if the list doesn't, then your program is allowed to fail with a null pointer exception).

The deleteAfter() method takes a reference to a node that is part of a linked list. This method should remove from the linked list the node after the selected node. Your method can assume that there must be a node after the selected node. This method should return the data item that was contained in the removed node.

The insertAfter method also takes a reference to a node that is part of a linked list. This method should insert a new node into the linked list right after the selected node. The data for the new node is given by the item parameter.

The append method is the most complicated of these methods. It takes references to the first nodes of two linked lists. You need to add, to the end of the first list, copies of all the nodes from the second list. Don't just point the last node of the first list at the first node of the second list. If you do that, then changes made to the second list will affect the new combined list. The new combined list must be independent of the original second list. The first step of your method should be to make a copy of the second list. Then you can point the last node of the first list at the first node of the copied list.

Turn in a zip file called CS275Hw3Surname.zip (where Surname is your last name) containing your version of LinkedListMethods.java.

This assignment is due Friday, December 10.