Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 current = head;

LinearNode newNode = new LinearNode(element);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions

Question

Discuss HR scorecard in detail.

Answered: 1 week ago

Question

What tools might be helpful?

Answered: 1 week ago