Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * lab goals: - Learn all about "do it yourself" linked lists
- * - Learn how to create unit tests using JUnit and the Jamtester tool.
- * - Learn how to create a complete battery of tests so as to attain complete "code coverage".
- * - Have a lot of fun looking at the bright colors red and green!
- *
- * lab instructions:
- * while ( everyMethodNotYetWrittenAndThoroughlyTested() ) {
- * 1) Write a test for a method.
- * 2) Run and watch the test fail.
- *
- * while ( testNotYetGreen() ) {
- 3) Write/modify the code for the method.
- * 4) re-run the test.
- * }
- * }
- *
- * Now that you have at least one test method per method in your class, go back and run
- * all tests in the student tool. Do you have complete code coverage? If not, design more
- * tests until you do.
- *
- * class invariant: elements stored in the list are all of the same type.
- */
- import java.util.*;
- import java.io.PrintStream;
- import java.lang.Comparable;
- import java.lang.Object;
- import java.lang.String;
- public class SinglyLinkedList2 extends SinglyLinkedList
- {
- /** returns the number of nodes in the list **/
- public int size()
- {
- return size(getFront());// so it compiles
- }
- private int size(ListNode curNode) // what is this method for?
- {
- int size = 1;
- while(curNode.getNext() != null)
- {
- curNode = curNode.getNext();
- size++;
- }
- return size;
- }
- /** adds c immediately before the getFront() node larger than it
- ** <br>all nodes before c are smaller than c
- ** <br>that is c.compareTo(get(i)) > 0
- **/
- public void inOrderInsert(Comparable c)
- {
- int ind = 0;
- ListNode curr = getFront();
- while (c.compareTo(curr.getNext().getValue()) < 0 && curr != null)
- {
- curr = curr.getNext();
- ind++;
- }
- add(ind +1 , c);
- }
- /** adds value to the end of the list.**/
- public void addLast(Object value)
- {
- assertValidType(value);
- int ind = 0;
- ListNode curr = getFront();
- while(curr.getNext() != null)
- {
- curr = curr.getNext();
- ind++;
- }
- add( ind +1, value);
- }
- /** returns a reference to the last node in a list**/
- public Object getLast()
- {
- ListNode node = getFront();
- while(node.getNext() != null)
- {
- node = node.getNext();
- }
- return node.getValue();
- // so it compiles
- }
- /** reverse the contents of the list.**/
- public void reverse()
- {
- for(int i = 0; i < size(); i++)
- {
- }
- }
- /** returns true if true iff getvalue().equals(obj) for some node in list<br>
- ** false otherwise **/
- public boolean contains(Object obj)
- {
- ListNode node = getFront();
- while(node != null)
- {
- if(node.getValue().equals(obj))
- {
- return true;
- }
- node = node.getNext();
- }
- return false;
- }
- /**returns the a reference to the middle node (or either of two middle nodes)
- **<br> returns null if the list is empty **/
- public ListNode middleNode()
- {
- if(getFront() == null)
- {
- return null;
- }
- ListNode node = getFront();
- for (int i = 0; i < size()/2; i++)
- {
- node = node.getNext();
- }
- return node;
- // so it compiles
- }
- }
Add Comment
Please, Sign In to add comment