Kind of confuse with this assignment, can anyone help Please! Write the followin
ID: 3540350 • Letter: K
Question
Kind of confuse with this assignment, can anyone help Please!
Write the following methods on the LinkedList class. The methods should not call any other methods in your LinkedList class(that is, for example, if you need to add a node to the linked list, don't call insert method; instead, do the insertion inside the methods you will write).
/**Adds the elements in otherlist to this list.
* Returns true if this list changed as a result of the call */
public Boolean addAll(MyList otherList)
/** Removes all the elments in otherList from this list
* Returns true if this list changed as a result of the call */
public Boolean removeAll(MyList otherList)
/** Retains the elements in this list that are also in otherList
* Returns true if this list changed as a result of the call */
public Boolean retainAll(myList otherList)
Ok this is how I would do it if i could call other method, but I am not sure how to do it without clling for another method
public boolean addAll(MyList otherList) {
if (otherList != null) {
addAll(otherList);
return true;
} else
return false;
}
public boolean removeAll(MyList otherList) {
if (otherList != null) {
removeAll(otherList);
return true;
} else
return false;
}
public boolean retainAll(MyList otherList) {
if (otherList != null) {
retainAll(otherList);
return true;
} else
return false;
}
Thanks
Explanation / Answer
Hey. I went through certain sources and found this is highly impossible. Reason being, a LinkedList is in an inbuilt class which extendsList class and finally the List class implements collection interface. Collection interface is already defined as well. So there are defined methods in collection interface like add(), addall(), retain(), retainall(), removeall(). As all these classes and Interface is predefined, we can NOT add anything unless we use these methods. At the max, we can override methods, we can NOT change the contents of LinkedList or List classes or Collection interface. So it not possible unless we use the inbuilt methods of the linked list class.
Hope your doubt is clear.
Let me know, if you have any more confusion
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.