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

Hey eveyone I need help with this java thing I am working on. All help is greatl

ID: 3812088 • Letter: H

Question

Hey eveyone I need help with this java thing I am working on. All help is greatly appreciated! If you can include comments too that'll be a big help! Thanks in advance!

You will implement a referenced based generic class, LinkedList, that represents an unordered Singly Linked List. The Singly Linked List will have a dummy head node. The Class should also implement the interface List from the Java API. However, since the List interface contains a lot of methods, some of which we haven’t covered, you need to implement only the following methods:

add(E o)

add(int index, E element)

clear()

contains(Object o)

get(int index)

indexOf(Object o)

isEmpty()

remove(int index)

size()

For all other methods throw UsupportedOperationException.

Explanation / Answer

Creating a dummy header :
private transient Entry header = new Entry(null, null, null);

public LinkedList() {

      header.next = header.previous = header;

}

Overridden functions (each function starting from 1) :

int size()

{

return size;

}

boolean isEmpty()

{

     if(size==0) return true;

     return false;

}

int indexOf(Object o)

{

       int index = 0;

       if (o==null) {

           for (Entry e = header.next; e != header; e = e.next) {

               if (e.element==-1)

                   return index;

               index++;

           }

       }

       else {

           for (Entry e = header.next; e != header; e = e.next) {

               if (o.equals(e.element))

                   return index;

               index++;

           }

       }

       return -1;

}

boolean contains(Object o) {

return indexOf(o) != -1;

}

boolean remove(int index)

{

       if (index < 0 || index >= size) {

           return false;

       }

       Entry e=get(index);

       remove(e);

       return true;

}

void clear()

{

      Entry e = header.next;

       while (e != header) {

           Entry next = e.next;

           e.next = e.previous = null;

           e.element = null;

           e = next;

       }

       header.next = header.previous = header;

       size = 0;

}

   

Element get(int index){

       if (index < 0 || index >= size)

           throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);

       Entry e = header;

       if (index < (size >> 1)) {

           for (int i = 0; i <= index; i++)

               e = e.next;

       } else {

           for (int i = size; i > index; i--)

               e = e.previous;

       }     

     return e.element;

   }

   

   void add(int index, Element element)

   {

     addBefore(element, (index==size ? header : entry(index)));

   }

   

   

   void add(Element element){

     addBefore(element, header);

   }

   private Entry addBefore(Element e, Entry entry) {

       Entry newEntry = new Entry (e, entry, entry.previous);

       newEntry.previous.next = newEntry;

       newEntry.next.previous = newEntry;

       size++;

       return newEntry;

   }

   

private Element remove(Entry e) {

       if (e == header)

           throw new NoSuchElementException();

       Element result = e.element;

       e.previous.next = e.next;

       e.next.previous = e.previous;

       e.next = e.previous = null;

       e.element = null;

       size--;

       return result;

   }

For all other methods, UnsupportedOperationException can be thrown by using reflection :

import java.lang.reflect.Method;

Method methodToFind = null;

try {

methodToFind = LinkedList.class.getMethod("myMethod", (Class<?>[]) null);

} catch (UnsupportedOperationException e) {

   // Your exception handling goes here

}