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

This code needs to be written in Java.. Thank you. For this assignment you will

ID: 3810384 • Letter: T

Question

This code needs to be written in Java.. Thank you.

For this assignment 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

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class SinglyLinkedList<T> implements List<T> {
@SuppressWarnings("hiding")
   class Node<T> {
T data;
Node<T> next;
Node (T data) {
this.data = data;
next = null;
}
public T getData(){
   return data;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
       public void setNext(Node node){
   next = node;
}
@SuppressWarnings("rawtypes")
       public Node getNext(){
   return next;
}
}
  
public SinglyLinkedList () {
head = null;
size = 0;
}

   @SuppressWarnings("rawtypes")
Node head;
  
   private int size = 0;

   public Node<T> getHead() {
       return head;
   }


   public boolean add(T element) {
       Node<T> node = new Node<T>(element);
       if (head == null)
           head = node;
       else {
           Node<T> p, q;
           for(p = head; (q = p.getNext()) != null; p = q);
               p.setNext(node);
       }
       size ++;
       return true;
   }
  
   public void add(int index, T element) {
       // fix the position
       if (index < 0) {
           index = 0;
       }
       if (index > size) {
           index = size;
       }

       if (head == null) {
           head = new Node<T>(element);
       }
       else if (index == 0) {
           Node<T> temp = new Node<T>(element);
           temp.next = head;
           head = temp;
       }
       else {
           Node<T> temp = head;
           for (int i=1; i<index; i+=1) {
               temp = temp.getNext();
           }
           Node<T> newNode = new Node<T>(element);
           newNode.next = temp.next;
           temp.setNext(newNode);
       }
       size += 1;
   }
  
  
   @SuppressWarnings("unchecked")
   public T remove(int position) {
       T t = null;
       if (position < 0) {
           position = 0;
       }
      
       if (position >= size) {
           position = size-1;
       }
      
       if (head == null)
           return t;
      
       if (position == 0) {
           head = head.getNext();
           t = (T)head.getData();
       }
       else {
           Node<T> temp = head;
           for (int i=1; i<position; i+=1) {
               temp = temp.getNext();
               t = temp.getData();
           }
           temp.setNext(temp.getNext().getNext());
       }
       size -= 1;
       return t;
   }
  
   // Return a string representation of this collection, in the form ["str1","str2",...].
   public String toString() {
       String result = "[";
       if (head == null) {
           return result+"]";
       }
       result = result + head.getData();
       Node<T> temp = head.getNext();
       while (temp != null) {
           result = result + "," + temp.getData();
           temp = temp.getNext();
       }
       return result + "]";
   }
  
   // Return the current size of the list.
   public int size() {
       return size;
   }
  
   public int indexOf(Object o) {
       // go looking for the data
       Node<T> temp = head;
       int pos = 0;
       while (temp != null) {
           if (temp.data.equals((T)o)) {
               return pos;
           }
           pos += 1;
           temp = temp.getNext();
       }
       return Integer.MIN_VALUE;
   }
  
   public void clear(){
       head = null;
       size = 0;
   }

   @Override
   public boolean contains(Object o) {
       T t = null;      
       if (head == null)
           return false;
       Node<T> temp = head;
       for (int i=0; i<size; i+=1) {
           temp = temp.getNext();
           t = temp.getData();
           if(t.equals(o))
               return true;
       }
       return false;
   }

   @SuppressWarnings("unchecked")
   @Override
   public T get(int index) {
       T t = null;
       if (index < 0) {
           index = 0;
       }
      
       if (index >= size) {
           index = size-1;
       }
      
       if (head == null)
           return t;
      
       if (index == 0) {
           head = head.getNext();
           t = (T)head.getData();
       }
       else {
           Node<T> temp = head;
           for (int i=1; i<index; i+=1) {
               temp = temp.getNext();
               t = temp.getData();
           }
       }
       size -= 1;
       return t;
   }

   @Override
   public boolean isEmpty() {
       if(size>0)
           return true;
       return false;
   }

  
  
   @Override
   public boolean addAll(Collection<? extends T> c) {
       throw new UnsupportedOperationException();
   }

   @Override
   public boolean addAll(int index, Collection<? extends T> c) {
       throw new UnsupportedOperationException();
   }

   @Override
   public boolean containsAll(Collection<?> c) {
       throw new UnsupportedOperationException();
   }

   @Override
   public Iterator<T> iterator() {
       throw new UnsupportedOperationException();
   }

   @Override
   public int lastIndexOf(Object o) {
       throw new UnsupportedOperationException();
   }

   @Override
   public ListIterator<T> listIterator() {
       throw new UnsupportedOperationException();
   }

   @Override
   public ListIterator<T> listIterator(int index) {
       throw new UnsupportedOperationException();
   }

   @Override
   public boolean remove(Object o) {
       throw new UnsupportedOperationException();
   }

   @Override
   public boolean removeAll(Collection<?> c) {
       throw new UnsupportedOperationException();
   }

   @Override
   public boolean retainAll(Collection<?> c) {
       throw new UnsupportedOperationException();
   }

   @Override
   public T set(int index, T element) {
       throw new UnsupportedOperationException();
   }

   @Override
   public List<T> subList(int fromIndex, int toIndex) {
       throw new UnsupportedOperationException();
   }

   @Override
   public Object[] toArray() {
       throw new UnsupportedOperationException();
   }

   @Override
   public <T> T[] toArray(T[] a) {
       throw new UnsupportedOperationException();
   }
}