# having trouble getting full average and splitting a LinkedList

• October 22nd, 2012, 08:59 AM
orbin
having trouble getting full average and splitting a LinkedList
I'm working with on a LinkedList assignment, however I've written the methods for splitting the list into two equal lists, but when I apply the average it only applies it to the first half of the list. When I try to calculate the average before I split the list, it gives me a null error, but I know I wrote the method correct because it works if I calculate the average after.

Here is my code.

Code :

```import java.util.Random;   public class TestLinkedList{     public static void main(String[] args){ UnorderedLinkedList list = new UnorderedLinkedList(); int sum=0; Random rand = new Random(); for(int i = 0; i<59; i++){ int randomNum = rand.nextInt(100)+1; sum = sum + randomNum; list.insertLast(new IntElement(randomNum));   } System.out.println("Printing the list."); list.print();   System.out.println("Deleting the first node."); list.deleteFront(); list.print();   System.out.println("Deleting the last node."); list.deleteLast(); list.print();   System.out.println("Inserting node into the front with a number of 68."); list.insertFirst(new IntElement(68)); list.print();   System.out.println("Inserting node into the back with a number of 75."); list.insertLast(new IntElement(75)); list.print();         System.out.println("Now splitting the list into to two equal lists."); list.splitMid(list); list.print(); //shows the first half of the list   System.out.println("The average is " + list.average()); System.out.println(sum/59); System.out.println();   } }```

Code :

```public abstract class LinkedList{     protected class LinkedListNode{ // inner class node definition protected DataElement info; protected LinkedListNode link; }   protected LinkedListNode first; //variable to store the address of the first node of the list protected LinkedListNode last; // variable to store the address of the last node of the list   protected int count; //variable to store the number of nodes in the list   //default constructor //initializes the list to an empty state. //postcondition: first = null, last = null, count = 0   public LinkedList(){ first = null; // last = null; //empty list count = 0; // }   public LinkedList(LinkedList list) { copy(list); }   private void copy(LinkedList list) { LinkedListNode pointer; LinkedListNode newNode;   if(list.first == null) { first = null; last = null; count = 0; } else { count = list.count; pointer = list.first; first = new LinkedListNode(); first.info = pointer.info.getCopy(); first.link = null; last = first; pointer = pointer.link;   while(pointer != null) { newNode = new LinkedListNode(); newNode.info = pointer.info.getCopy(); newNode.link = null; last.link = newNode; last = newNode; pointer = pointer.link; } } }       //method to determine whether the list is empty //postcondition: returns true if the list is empty; false otherwise     public boolean isEmptyList(){ return (count == 0); }       //method to initialize the list to an empty state //postcondition: first = null, last = null, count = 0   public void initializeList(){ first = null; last = null; count = 0; }     //method to output the data contained in each node   public void print(){   if(count == 0){ System.out.println("Cannot print an empty list."); } else{   LinkedListNode pointer = new LinkedListNode(); pointer = first; while(pointer != null){     System.out.println(pointer.info.toString()); pointer=pointer.link; }   } System.out.println(); }     //method to return the number of nodes in the list. //postcondition: the value of count is returned   public int length(){ return count; }   //method to return a reference of the object //containing the data of the first node of the list //precondition: the list must exist and must not be empty. //postcondition: the reference of the object that contains //the info of the first node is returned   public DataElement front(){     return first.info.getCopy(); }     //Method to return a reference of object containing //the data of the last node of the list. //Precondition: The list must exist and must not be empty. //Postcondition: The reference of the object that //contains the info of the last node //is returned. public DataElement back(){ return last.info.getCopy(); }   //Method to insert newItem in the list. //Postcondition: first points to the new list // and newItem is inserted at the // beginning of the list. Also, // last points to the last node and // count is incremented by 1. public void insertFirst(DataElement newItem){ LinkedListNode newNode = new LinkedListNode(); newNode.info = newItem.getCopy(); newNode.link = first; first = newNode;   if(last == null){   last = newNode; count++; }     }   //Method to insert newItem at the end of the list. //Postcondition: first points to the new list and //newItem is inserted at the end //of the list. Also, last points to //the last node and //count is incremented by 1. public void insertLast(DataElement newItem){   LinkedListNode newNode = new LinkedListNode(); newNode.info = newItem.getCopy(); newNode.link = null; if(first == null){ first = newNode; last = newNode; } else{   last.link = newNode; last = newNode; count++; } }   public void deleteFront() { if(first.link != null) { first = first.link; count--; } else { first = null; last = null; count = 0; } }   public void deleteLast() { LinkedListNode pointer = first; if(pointer.link == null) { first = null; last = null; count = 0; } else { while(pointer.link.link != null) pointer = pointer.link; pointer.link = null; last = pointer; count--; } }   public void splitMid(LinkedList list) { int stopPoint = list.count / 2; LinkedListNode pointer = null; pointer = list.first.link; for(int i = 0; i < stopPoint; i++) { pointer = pointer.link; } LinkedListNode secondFirst = pointer.link;   pointer.link.link = null;   }     public double average(){ int sum=0; LinkedListNode pointer = new LinkedListNode();   while(pointer!=null){   pointer = first; pointer.link = first.link; System.out.println("Method avg pointer = " + pointer + " first = " + first); IntElement temp = (IntElement)pointer.info.getCopy(); sum = sum + temp.getNum(); first = pointer.link; if(pointer.link.link == null) { break; } else{     first.link = pointer.link.link; System.out.println(sum); }   } double average = (sum/59); return average;   }     //Method to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is found //in the list; false otherwise. public abstract boolean search(DataElement searchItem);   //Method to delete deleteItem from the list. //Postcondition: If found, the node containing //deleteItem is deleted from the //list. Also first points to the first //node, last points to the last //node of the updated list, and count //is decremented by 1. public abstract void deleteNode(DataElement deleteItem);         }```

Code :

```public class UnorderedLinkedList extends LinkedList{     public UnorderedLinkedList(){ super(); }   public UnorderedLinkedList(UnorderedLinkedList list){ super(list); }   //Method to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is found //in the list; false otherwise. public boolean search(DataElement searchItem){   LinkedListNode pointer = new LinkedListNode(); pointer = first; boolean foundData = false;   while(pointer != null && !foundData){ if(pointer.info.equals(searchItem) == true){ foundData = true; } else{ pointer = pointer.link; }   } return foundData;   }     //Method to delete deleteItem from the list. //Postcondition: If found, the node containing //deleteItem is deleted from the //list. Also first points to the first //node, last points to the last //node of the updated list, and count //is decremented by 1. public void deleteNode(DataElement deleteItem){   if(count == 0) System.out.println("The list is empty."); else{ if(first.info.equals(deleteItem) == true) { if(first.link == null){ first = null; last = null; count = 0; } else{ first = first.link; count--; if(count == 1){ first.link = null; last = first; } } }   else{ LinkedListNode pointer = new LinkedListNode(); pointer = first;   while(pointer.info.equals(deleteItem) != true){ pointer = pointer.link; pointer.link = pointer.link.link; //inception   if(pointer.link == null){ last = pointer; count--; }   }     } }         }   }```

Code :

```public abstract class DataElement{   public abstract boolean equals(DataElement otherElement);   public abstract int compareTo(DataElement otherElement);   public abstract void makeCopy(DataElement otherElement);   public abstract DataElement getCopy();     }```

Code :

```public class IntElement extends DataElement {   protected int number;   public IntElement() { number = 0; }   public IntElement(int num) { number = num; }   public IntElement(IntElement obj) { this.number = obj.getNum();   }   public void setNum(int num) {   number = num; }   public String toString() { return Integer.toString(number); }   public int getNum() {   return number; }   public boolean equals(DataElement obj) {   if (obj == null) return false; if (getClass() != obj.getClass()) return false; IntElement temp = (IntElement)obj; return (number == temp.number); }   public int compareTo(DataElement obj) { IntElement temp = (IntElement)obj; if (number < temp.number) return -1; else if (number > temp.number) return 1; else return 0; }   public void makeCopy(DataElement obj) { IntElement temp = (IntElement)obj; number = temp.number; }   public DataElement getCopy() { DataElement temp = new IntElement(number); return temp; }   }```

edit: I just figured out by using the constructor I can make a copy of the list. I was trying to copy using LinkedList object when it's abstract instead of creating a Unordered object. My average method is having trouble. It keeps cutting off the last node's number. Why is it doing this? I'm trying to figure out how to fix it.
Code :

```import java.util.Random;   public class TestLinkedList{     public static void main(String[] args){ UnorderedLinkedList list = new UnorderedLinkedList(); int sum=0; Random rand = new Random(); for(int i = 0; i<59; i++){ int randomNum = rand.nextInt(100)+1; sum = sum + randomNum; list.insertLast(new IntElement(randomNum));   } System.out.println("Printing the list."); list.print();   System.out.println("Deleting the first node."); list.deleteFront(); list.print();   System.out.println("Deleting the last node."); list.deleteLast(); list.print();   System.out.println("Inserting node into the front with a number of 68."); list.insertFirst(new IntElement(68)); list.print();   System.out.println("Inserting node into the back with a number of 75."); list.insertLast(new IntElement(75)); list.print();   UnorderedLinkedList copyList = new UnorderedLinkedList(list);   System.out.println("Now splitting the list into to two equal lists."); list.splitMid(list); list.print(); //shows the first half of the list   System.out.println("The average is " + copyList.average()); System.out.println(sum/59.0); System.out.println();       } }```

Code :

```The average is 51.23728813559322 52.389830508474574```
• October 22nd, 2012, 02:58 PM
orbin
Re: having trouble getting full average and splitting a LinkedList
Been racking my brain. I can't figure out now why my average and stdDeviation method aren't working anymore.

Code :

```public abstract class LinkedList{     protected class LinkedListNode{ // inner class node definition protected DataElement info; protected LinkedListNode link; }   protected LinkedListNode first; //variable to store the address of the first node of the list protected LinkedListNode last; // variable to store the address of the last node of the list   protected int count; //variable to store the number of nodes in the list   //default constructor //initializes the list to an empty state. //postcondition: first = null, last = null, count = 0   public LinkedList(){ first = null; // last = null; //empty list count = 0; // }   public LinkedList(LinkedList list) { copy(list); }   private void copy(LinkedList list) { LinkedListNode pointer; LinkedListNode newNode;   if(list.first == null) { first = null; last = null; count = 0; } else { count = list.count; pointer = list.first; first = new LinkedListNode(); first.info = pointer.info.getCopy(); first.link = null; last = first; pointer = pointer.link;   while(pointer != null) { newNode = new LinkedListNode(); newNode.info = pointer.info.getCopy(); newNode.link = null; last.link = newNode; last = newNode; pointer = pointer.link; } } }       //method to determine whether the list is empty //postcondition: returns true if the list is empty; false otherwise     public boolean isEmptyList(){ return (count == 0); }       //method to initialize the list to an empty state //postcondition: first = null, last = null, count = 0   public void initializeList(){ first = null; last = null; count = 0; }     //method to output the data contained in each node   public void print(){   if(count == 0){ System.out.println("Cannot print an empty list."); } else{   LinkedListNode pointer = new LinkedListNode(); pointer = first; while(pointer != null){     System.out.println(pointer.info.toString()); pointer=pointer.link; }   }   }     //method to return the number of nodes in the list. //postcondition: the value of count is returned   public int length(){ return count; }   //method to return a reference of the object //containing the data of the first node of the list //precondition: the list must exist and must not be empty. //postcondition: the reference of the object that contains //the info of the first node is returned   public DataElement front(){     return first.info.getCopy(); }     //Method to return a reference of object containing //the data of the last node of the list. //Precondition: The list must exist and must not be empty. //Postcondition: The reference of the object that //contains the info of the last node //is returned. public DataElement back(){ return last.info.getCopy(); }   //Method to insert newItem in the list. //Postcondition: first points to the new list // and newItem is inserted at the // beginning of the list. Also, // last points to the last node and // count is incremented by 1. public void insertFirst(DataElement newItem){ LinkedListNode newNode = new LinkedListNode(); newNode.info = newItem.getCopy(); newNode.link = first; first = newNode;   if(last == null){   last = newNode; count++; }     }   //Method to insert newItem at the end of the list. //Postcondition: first points to the new list and //newItem is inserted at the end //of the list. Also, last points to //the last node and //count is incremented by 1. public void insertLast(DataElement newItem){   LinkedListNode newNode = new LinkedListNode(); newNode.info = newItem.getCopy(); newNode.link = null; if(first == null){ first = newNode; last = newNode; } else{   last.link = newNode; last = newNode; count++; } }   public void deleteFront() { if(first.link != null) { first = first.link; count--; } else { first = null; last = null; count = 0; } }   public void deleteLast() { LinkedListNode pointer = first; if(pointer.link == null) { first = null; last = null; count = 0; } else { while(pointer.link.link != null) pointer = pointer.link; pointer.link = null; last = pointer; count--; } }   public void splitMid(LinkedList list) { int stopPoint = list.count / 2; LinkedListNode pointer = null; pointer = list.first.link; for(int i = 0; i < stopPoint; i++) { pointer = pointer.link; } LinkedListNode secondFirst = pointer.link;   pointer.link.link = null;   }     public double average(){ double sum=0; LinkedListNode pointer = new LinkedListNode();   while(pointer != null){   pointer = first; pointer.link = first.link; IntElement temp = (IntElement)pointer.info.getCopy(); sum = sum + temp.getNum(); System.out.println("temp num: " + temp.getNum()); first = pointer.link; if(pointer.link.link != null){   first.link = pointer.link.link; } else{ pointer = first; IntElement temp2 = (IntElement)pointer.info.getCopy(); sum = sum + temp.getNum(); break; } }   double average = (sum/59.0); return average;   }   public double stdDev(double avg){ double stdAvg = avg; double variance = 0; LinkedListNode pointer = new LinkedListNode();   while(pointer!= null){   pointer = first; pointer.link = first.link; IntElement temp = (IntElement)pointer.info.getCopy(); variance = variance + ((temp.getNum() - stdAvg)*(temp.getNum() - stdAvg));   first = pointer.link; if(pointer.link.link != null){   first.link = pointer.link.link; } else{ pointer = first; IntElement temp2 = (IntElement)pointer.info.getCopy(); variance = variance + ((temp.getNum() - stdAvg)*(temp.getNum() - stdAvg));     } } return (Math.sqrt(variance/59)); }     //Method to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is found //in the list; false otherwise. public abstract boolean search(DataElement searchItem);   //Method to delete deleteItem from the list. //Postcondition: If found, the node containing //deleteItem is deleted from the //list. Also first points to the first //node, last points to the last //node of the updated list, and count //is decremented by 1. public abstract void deleteNode(DataElement deleteItem);         }```

Code :

```import java.util.Random;   public class TestLinkedList{     public static void main(String[] args){ UnorderedLinkedList list = new UnorderedLinkedList(); Random rand = new Random(); for(int i = 0; i<=59; i++){ int randomNum = rand.nextInt(100)+1; list.insertLast(new IntElement(randomNum));   }       UnorderedLinkedList copyList = new UnorderedLinkedList(list); //a copy of the linked list to use   System.out.println("The average is: " + list.average());   System.out.println("The Standard Deviation is: " + list.stdDev(list.average()));   System.out.println("Printing the list."); list.print();   System.out.println("Deleting the first node."); list.deleteFront(); list.print();   System.out.println("Deleting the last node."); list.deleteLast(); list.print();   System.out.println("Inserting node into the front with a number of 68."); list.insertFirst(new IntElement(68)); list.print();   System.out.println("Inserting node into the back with a number of 75."); list.insertLast(new IntElement(75)); list.print();   System.out.println("Now splitting the list into to two equal lists."); list.splitMid(list); list.print(); //shows the first half of the list         } }```

Code :

```temp num: 41 temp num: 4 temp num: 2 temp num: 1 The average is: 49.50847457627118 temp num: 41 Exception in thread "main" java.lang.NullPointerException at LinkedList.average(LinkedList.java:242) at TestLinkedList.main(TestLinkedList.java:21)   Process completed.```
• October 22nd, 2012, 07:24 PM
jps
Re: having trouble getting full average and splitting a LinkedList
It is easier to find problems and give advice if you provide a detailed explanation about what the code is doing, what you want it to do, and any error messages you may have. To say some number of things "aren't working anymore" is not very descriptive. Though it does suggest these things did work before. Which suggests there may be some code recently changed and caused them to stop working. But that is the best I have to offer without more to go on, or spending hours racking my brain too.