/* Garth Bruen A very simple linked list to go along with the sorting of two integer lists Since were only creating lists, there are no methods for deleting, inserting or searching for items. */ public class PP_1_2{ private Node startOfList = null; public void addTo(int data){ Node newNode = new Node(); newNode.data = data; newNode.next = startOfList; startOfList = newNode; }//end addFirst() class Node{ int data; Node next = null; }//end Node() /* This is the method that does the work we need. It takes the original list and splits it into two, one odd, one even. To do this it creates two new lists then looks through the original one and tries to determine if the number is odd or even. It does this by dividing each number by two to see if there is a remainder, using the mod operator %. */ public void makeTwoLists(){ PP_1_2 odd = new PP_1_2(); //New lists PP_1_2 even = new PP_1_2(); Node current = startOfList; while(current != null){ if((current.data%2)==0){ even.addTo(current.data); //if so put in list } //Is it even? else{ if((current.data%2)==1){ odd.addTo(current.data); //if so put in list } }//Or is it odd? current = current.next; //Get more values } even.printList(); //Print out new lists odd.printList(); }//end makeTwoLists() /* Method loops through list to print it out */ public void printList(){ Node current = startOfList; while(current != null){ System.out.print(current.data); current = current.next; } System.out.println(); }//end printList() public static void main(String args[]) throws Exception { PP_1_2 unsorted = new PP_1_2(); unsorted.addTo(1); unsorted.addTo(2); unsorted.addTo(3); unsorted.addTo(4); unsorted.addTo(5); unsorted.addTo(6); unsorted.addTo(7); unsorted.addTo(8); unsorted.addTo(9); unsorted.makeTwoLists(); }//End main }