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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started