Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For my code I had to fill in the three methods of addFront, addRear, & addAfter.

ID: 3799542 • Letter: F

Question

For my code I had to fill in the three methods of addFront, addRear, & addAfter. I keep getting an error however and I'm not sure why. Below is my errr and code

"LinkedUnorderedList.java:86: error: illegal start of type
               if(!found))
               ^
LinkedUnorderedList.java:86: error: illegal start of type
               if(!found))
                  ^
LinkedUnorderedList.java:86: error: ')' expected
               if(!found))
                   ^
LinkedUnorderedList.java:86: error: ';' expected
               if(!found))
                        ^
LinkedUnorderedList.java:86: error: illegal start of type
               if(!found))
                         ^
LinkedUnorderedList.java:86: error: <identifier> expected
               if(!found))
                          ^
LinkedUnorderedList.java:87: error: ';' expected
                  throw new ElementNotFoundException("List");
                       ^
LinkedUnorderedList.java:87: error: invalid method declaration; return type required
                  throw new ElementNotFoundException("List");
                            ^
LinkedUnorderedList.java:87: error: illegal start of type
                  throw new ElementNotFoundException("List");
                                                     ^
LinkedUnorderedList.java:89: error: <identifier> expected
                  beforeCurent.setNext(temp);
                                      ^
LinkedUnorderedList.java:89: error: <identifier> expected
                  beforeCurent.setNext(temp);
                                           ^
LinkedUnorderedList.java:90: error: <identifier> expected
                  temp.setNext(current);
                              ^
LinkedUnorderedList.java:90: error: <identifier> expected
                  temp.setNext(current);
                                      ^
LinkedUnorderedList.java:92: error: illegal start of type
                  if(BeforeCurrent == tail)
                  ^
LinkedUnorderedList.java:92: error: <identifier> expected
                  if(BeforeCurrent == tail)
                                  ^
LinkedUnorderedList.java:92: error: ';' expected
                  if(BeforeCurrent == tail)
                                     ^
LinkedUnorderedList.java:92: error: illegal start of type
                  if(BeforeCurrent == tail)
                                          ^
LinkedUnorderedList.java:95: error: <identifier> expected
                  count++;
                       ^
LinkedUnorderedList.java:97: error: class, interface, or enum expected
}
"

/**
* LinkedUnorderedList represents a singly linked implementation of an
* unordered list.
*
* @author Lewis and Chase
* @version 4.0
*/
public class LinkedUnorderedList<T> extends LinkedList<T> implements UnorderedListADT<T>
{
    /**
     * Creates an empty list.
     */
    public LinkedUnorderedList()
    {
        super();
    }

    /**
     * Adds the specified element to the front of this list.
     *
     * @param element the element to be added to the list
     */
    public void addToFront(T element)
    {
        // To be completed as a Programming Project
        LinearNode<T> temp = new LinearNode<T>(element);
       
        if(size() == 0)
            head = tail = temp;
           else{
           temp.setNext(head);
          
           head = temp;
     }
         count++;
     }
       
    /**
     * Adds the specified element to the rear of this list.
     *
     * @param element the element to be added to the list
     */
    public void addToRear(T element)
    {
        // To be completed as a Programming Project
        LinearNode<T> temp = new LinearNode<T>(element);
       
        if(size() == 0)
         head = tail = temp;
        else{
         tail.setNext(temp);
        
         tail = tail.getNext();
    }
      count++;
   }
       
       
    /**
     * Adds the specified element to this list after the given target.
     *
     * @param element the element to be added to this list
     * @param target the target element to be added after
     * @throws ElementNotFoundException if the target is not found
     */
    public void addAfter(T element, T target)
    {
        // To be completed as a Programming Project
        if(isEmpty())
            throw new EmptyCollectionException("List");
           
            LinearNode<T> temp = new LinearNode<T>(element);
            LinearNode<T> current = head;
            LinearNode<T> beforeCurrent = null;
            boolean found = false;
           
            while(!found && current != tail.getNext())
           
               if(target.equals(current.getElement()))
                  found = true;
                 
               beforeCurrent = current;
               current = current.getNext();
               }
              
               if(!found)
                  throw new ElementNotFoundException("List");
                 
                  beforeCurent.setNext(temp);
                  temp.setNext(current);
                 
                  if(BeforeCurrent == tail)
                     tail = temp;
                    
                  count++;
    }  
}

.


Explanation / Answer

hi! there small mistakes in code. I corrected these mistakes. Add this code in your program

Mistakes are:

1. if(!found)) => if(!found)

2.beforeCurent.setNext(temp); => beforeCurrent.setNext(temp);

3.temp.setNext(curent); => temp.setNext(current);

4.if(BeforeCurrent == tail) => if(beforeCurrent == tail)

Code:

/**
* LinkedUnorderedList represents a singly linked implementation of an
* unordered list.
*
* @author Lewis and Chase
* @version 4.0
*/
public class LinkedUnorderedList<T> extends LinkedList<T> implements UnorderedListADT<T>
{
/**
* Creates an empty list.
*/
public LinkedUnorderedList()
{
super();
}
/**
* Adds the specified element to the front of this list.
*
* @param element the element to be added to the list
*/
public void addToFront(T element)
{
// To be completed as a Programming Project
LinearNode<T> temp = new LinearNode<T>(element);

if(size() == 0)
head = tail = temp;
else{
temp.setNext(head);
  
head = temp;
}
count++;
}

/**
* Adds the specified element to the rear of this list.
*
* @param element the element to be added to the list
*/
public void addToRear(T element)
{
// To be completed as a Programming Project
LinearNode<T> temp = new LinearNode<T>(element);

if(size() == 0)
head = tail = temp;
else
{
tail.setNext(temp);

tail = tail.getNext();
}
count++;
}


/**
* Adds the specified element to this list after the given target.
*
* @param element the element to be added to this list
* @param target the target element to be added after
* @throws ElementNotFoundException if the target is not found
*/
public void addAfter(T element, T target)
{
// To be completed as a Programming Project
if(isEmpty())
throw new EmptyCollectionException("List");

LinearNode<T> temp = new LinearNode<T>(element);
LinearNode<T> current = head;
LinearNode<T> beforeCurrent = null;
boolean found = false;

while(!found && current != tail.getNext())

if(target.equals(current.getElement()))
found = true;

beforeCurrent = current;
current = current.getNext();

  
if(!found)
throw new ElementNotFoundException("List");

beforeCurrent.setNext(temp);
temp.setNext(current);

if(beforeCurrent == tail)
tail = temp;
  
count++;
}
  
}