publicclass SortedLinkedList<T extends Comparable<T>> {
/** Singly linked list . */
private Node<T> head; // head node of the list
privatelong size; // number of nodes in the list
/** Default constructor that creates an empty list */
public SortedLinkedList() {
head = new Node<T>(null , null);;
size = 0;
}
// ... update and search methods would go here ...
/**
* Adds an element to the list in the proper place as defined by
* the element's compareTo
* @param newElement element to add to list
*/
publicvoid add(T newElement) {
Node<T> n;
for (n = head; n.getNext() != null; n = n.getNext())
if (newElement.compareTo(n.getNext().getElement()) < 0)
break;
Node<T> node = new Node<T>(newElement, n.getNext());
n.setNext(node);
++size;
}
/**
* Searches for element in list
* @param element element to search for
* @return true if element exists, false otherwise
*/
public boolean exists(T element) {
for (Node<T> n = head.getNext(); n != null; n = n.getNext())
if(n.getElement().compareTo(element) == 0)
returntrue;
returnfalse;
}
/**
* Renders list as string
* @return string that represent the list
*/
public String toString() {
String retStr = "";
for (Node<T> n = head.getNext(); n != null; n = n.getNext())
retStr += n.getElement() + "\n";
return retStr;
}
}
This is what I have so far. I have errors on line 30 and 38.