CSC232: Introduction to CS II
Homework Assignment # 2
Due: Beginning of class on Monday September 28th, 2009.
Students may work on this assignment in groups of two, in
which case only one hard copy with both names needs to be turned in. Also
remember to write down the name of the eccentric account that has the electronic
copy.
Download and modify the
SingleLinkedList<E> class as follows:
- Add a remove method with interface as specified in
programming exercise 3, page 221 of the text.
- Add an add method with interface as specified in
programming exercise 4, page 221 of the text. Note: index is out of range if
(index < 0 || index > size()). Note: after item has been added to the list,
its position/index in the list is equal to index
- Add a method, int indexOf(E target), that returns
either the position of the first occurrence of target in the list or -1 if a
copy of target is not in the list.
- Add a method, int size(), that returns number of
elements in the list. This method must operate in O(1) time, which means
that you need to add a size instance variable that will be incremented
whenever an item is added and decremented whenever an item is removed.
- Add an inner class, Iter, that implements the
Iterator<E> interface. Then add a method Iterator<E> iterator()
to the SingleLinkedList class. This method will allow you to iterate over
SingleLinkedList objects.
- Add a main method and test all the methods you have
written. Make sure to provide comprehensive testing including boundary
conditions/cases. For example, to completely test the method remove, your
must have code that removes from the beginning, end, and middle of the list
as well as removing an item that does not exist in the list.
Your code must be efficient, which means that you must use an
iterator when needed. Your code must appear at the beginning of the
SingleLinkedList class, after the instance variable. Turn in a
stapled hard copy at the beginning of class on the due date and make
sure to upload an electronic copy of the source file in a
subfolder named HW2 in your CSC232 eccentric folder.