0. Executive Summary

This is the fourth graded lab for CS2102.

1. Stated Aims

  1. Learn how to maintain a linked list in sorted order by controlling how nodes are added to the list. That is, instead of simply 'prepending' or 'appending' to the list, you will learn how to locate the proper spot in which to insert a node.

2. Background knowledge

  1. Understand concept of node in a linked list
  2. Understand concept of a linked list
  3. Understand how to traverse a linked list.

3. Setup

If you are in the Lab, perform the routine KH 202 setup or AK 120D setup; for now, ignore the "advanced setup" options. If you would like to set up  Eclipse on your personal computer, then (a) install Java JDK 1.5 if you need to as described in tools section (here); and (b) install Eclipse as described in tools section (here).

4. Individual Tasks

4.1 Work on example by hand

This example must be handed in to the lab proctors and is a record that you attended the lab.

Given the NumberList above, what is the resulting LinkedList structure after invoking insert(5) and then insert(1)? Draw it out in the same manner as shown above.

4.2 Design the insert method

Use Eclipse/Sourceforge to update Examples project to retrieve code files for package nov29. This package contains three classes: NumberNode, NumberList, and NumberListTest. For this lab, you must:

  1. Complete the implementation of the insert method in NumberList
  2. Complete the implementation of the testAdd method in the JUnit test case NumberListTest.

The documentation found in the class files should be sufficient to direct you in your endeavors.

4.3 Complete the JUnit test case to validate the insert method

This code is provided within the NumberListTest class. Fill in the designated code to test the operation of insert. You should use assertEquals() and use the result of invoking toString on a NumberList object.

4.4 Answer the following question (not required to be turned in)

In the insert method, there is a while loop to traverse the list. The guard on the while loop is

while (prev != null) {
    // body of loop.
}

Given the set of test cases in NumberListTest, which test case breaks if you had mistakenly used:

while (node != null) {
    // body of loop.
}

5. Follow-up Tasks

Your goal is to turnin the assignment file(s) (NumberList.java and NumberListTest.java) by November 29th at 11:59 PM. The name of the assignment you should use is "lab4".

6. Change History

Date Reason/Change
11/28/2006 Ready to go