CS367 Homework 4
Lecture 3, Fall 2015
Due by 11:59 pm on Friday, October 23 (not accepted late)

Announcements

Check here periodically.

10/13/2015  Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: https://piazza.com/wisc/fall2015/cs3673 and sign-in using your wisc.edu account.

Questions

Homework assignments must be done individually. Collaboration on homework assignments is not allowed.

Assume that a LinkedList<E> class has been implemented as a circular singly-linked chain of nodes with no (dummy) header node. The LinkedList<E> class has the following data members (note that there is no head data member):

private Listnode<E> tail;     // references the last node
private int numItems;

LinkedList<E> uses the Listnode<E> class given in the on-line reading.

Below is an implementation of a new method, moveToFront, for the LinkedList<E> class. This method moves the object at the specified position to the front of the list.

public void moveToFront( int position ) {
    this.add(0, this.remove(position)));
}

Recall that if a position is invalid, the remove method throws an IndexOutOfBoundsException.

For this homework, complete a second version of moveToFront that functions that same as the code above. However, your version directly changes the chain of nodes by unlinking the node to be moved and re-linking it into the front of the list.

Be sure that your code works for all cases such as when the list is empty, has just one object, or has more than one object. Also consider when the item to be moved is already at the front, is in the middle, or is at the end of the list.

Handing in

Please include your name at the top your file.

Put your answers to both questions into one file named Homework4 with the appropriate file extension, e.g., Homework3.pdf (see File Format for acceptable file formats).

Electronically submit your work to the Homework 3 Dropbox on Learn@UW.

Last Updated: 6/23/2014     © 2014 Beck Hasti