Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BIL 212

Fall 2021

Lab 1

Each week there will be an exercise part and a lab part. You need to get checked off by
your TA for the lab part.

First download SinglyLinkedList.java, SLLDriver.java, DoublyLinkedList.java and


DLLDriver.java from Lecture1-LinkedLists section under Resources on the course Piazza
site.

Exercise:

1. clone() : Within the SinglyLinkedList class, write a clone method as given in


the textbook, Chapter 3, that is, you will make a copy of the linked list, the copy
will have its own independent nodes, but the “element” references that are stored
in the copy nodes will be the same references as in the original list. See the
textbook for this kind of clone(). This is NOT a deep copy. We also wrote this
method in class, you can find it on the last few pages of the lecture1 slides.

2. Now, you will rewrite the clone method such that it makes a deep copy of the
linked list (both nodes and the elements of the copy will be independent from the
original list’s.) First try the following two steps:
(i) use the following code when making a new Node that is a copy of the node
referenced by variable walk:
Node<E> newNode = new Node<E>((E)(walk.getElement()).clone(),
null);

(ii) and, restrict type E in your class heading as follows since you want E to be
Cloneable:

public class SinglyLinkedList<E extends Cloneable> implements


Cloneable

If you try to compile this code you will get an error message saying that the
clone() is protected in class Object. This is because at this time all Java knows
that the generic type E is a descendent of class Object, and the clone() method in
Object class is protected.

Here is one way you can overcome this problem:

(i) First, define a new interface called PubliclyCloneable which contains a


public clone() method and extends Cloneable. Here is its definition:

public interface PubliclyCloneable extends Cloneable{


public Object clone()throws CloneNotSupportedException;
}

Any class that implements this interface needs to define a public clone() method.

(ii) Now, restrict type E in your class heading as:


public class SinglyLinkedList<E extends PubliclyCloneable>
implements PubliclyCloneable

(iii) Now, you can write a deep clone() method in SinglyLinkedList by making
nodes as suggested before:

Node<E> newNode = new Node<E>((E)(walk.getElement()).clone(),


null);

To test your deep clone() method, first define a simple Person class with two
instance variables name, and age. Let this class implement PubliclyCloneable and
provide a public clone() method by simply calling super.clone(). Then, make a
linked list of Person objects. Clone the linked list. And test to see if the Person
objects in the original list and the copy are really independent. For example, you can
change a Person object’s data in one list and see if it is changed or not in the copy.

Lab:

Add the following methods to the DoublyLinkedList class and test them by adding
appropriate statements into DLLDriver. Make sure to test all cases.

• private Node<E> getNode(int i) : returns the address of the i-th node.


The node immediately after the header sentinel is the 0-th node.
• public void add(int i, E newData): adds newData at the i-th
position. Write this method by making a call to the getNode method you have
written above and the addBetween method given in the class.
• public E remove(int i): removes the i-th node, and returns the data that
is removed. Write this method by making calls to the getNode method you have
written above, the remove method given in the class.
• public void removeEveryOther(): starting with the head node remove
every other node of the linked list. For example, if the list contains (A, B, C, D, E,
F) initially, the list will contain (B, D, F) after the removal operation. Write this
method by walking through the list ONLY once. Update the original list, do NOT
make a new list.

You might also like