Share
Explore BrainMass

Java Split Linked List Into Two Equal Sublists

See the attachment.

Splitting a Linked List into two sublists of almost equal sizes

Add the method splitMid to the class LinkedListClass as follows:
Public void splitMid(LinkedListClass<T> sublist);
// This method splits the given list into two sublists of almost equal sizes
// Precondition: The list must exist
//Postcondition: first points to the first node, and last points to the last node of the first sublist
// sublist.first points to the first node, and sublist.last points to the
// last node of the second sublist.

Consider the following statements:
UnorderedLinkedList<Integer> myList;
UnorderedLinkedList<Integer> sublist;

Suppose myList points to the list with elements 34, 65, 27, 89, and 12 (in this order). The statement
myList.splitMid(subList);
splits myList into two sublists: myList points to the list with elements 34, 65, and 27. And subList points to the sublist with elements 89 and 12.
Write the definition of the method splitMid. Also write a program to test your method. (Use either the class UnorderedLinkedList or the class OrderedLinkedList to test your method).

Attachments

Solution Preview

I have added the method splitMid() to the UnorderedArrayList.java. It is also declared in ...

Solution Summary

The solution discusses the Java split linked list into two equal sublists.

$2.19