Question: 5.(8 points) Given a singly linked list using the LinearNode class as described in class, write a method called addBefore that will add the given
5.(8 points) Given a singly linked list using the LinearNode class as described in class, write a method called addBefore that will add the given element in front of the given target in the list. You must not assume any other methods are available. You may assume that there is a pointer named head that points to the first element in the list. I have included the code for the addAfter method to give you a starting point.
public void addAfter(T element, T target)
{
if (isEmpty())
throw new ElementNotFoundException("LinkedUnorderedList");
boolean found = false;
LinearNode
LinearNode
while (current != null && !found)
if (target.equals(current.getElement()))
found = true;
else
current = current.getNext();
if (!found)
throw new ElementNotFoundException("LinkedUnorderedList");
newNode.setNext(current.getNext());
current.setNext(newNode);
count++;
}
public void addBefore(T element, T target)
{
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
