Java Write a contains method for a linked implementation of a sorted list. This
ID: 3605763 • Letter: J
Question
Java
Write a contains method for a linked implementation of a sorted list.
This method is written from the implementation perspective, meaning it goes inside of the SortedLinkedList class.
This means you have access to firstNode and numberOfEntries.
The method header is:
public boolean contains(T anEntry)
For full credit, write an efficient solution.
This will involve directly accessing the linked structure rather than only invoking existing methods.
You should also take advantage of the knowledge that the list is always sorted.
You can assume T is Comparable.
SortedLinkedList class file : http://www.mediafire.com/file/rsiwh42rjt9s0bp/HomeworkW10Driver.zip
Explanation / Answer
Please find my implementaton of required method.
public boolean contains(T anEntry) {
Node currentNode = firstNode;
while(currentNode != null) {
if(currentNode.getData().compareTo(anEntry) == 0)
return true;
currentNode = currentNode.getNextNode();
}
return false; // not found
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.