The code needs to be written in Java. For this assignment you will implement a g
ID: 3819842 • Letter: T
Question
The code needs to be written in Java. For this assignment you will implement a generic class, LinkedList, that represents an unordered list using an array, a.k.a. layered implementation. 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 UnsupportedOperationException.
Explanation / Answer
//Note pls paste this code in unimplementd methods in place of return statement if one exists or just place it at the end.
// throw new UnsupportedOperationException("not yet impelented");
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class LinkedList<E> implements List<E>
{
private E[] a;
int count=0;
public LinkedList()
{
a = (E[]) new Object[10];
}
@Override
public int size() {
// TODO Auto-generated method stub
return count;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return count==0;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
E other=(E)o;
for(int i=0;i<count;i++)
{
if(a[i].equals(other))
{
return true;
}
}
return false;
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(E e) {
// TODO Auto-generated method stub
if(count==a.length)
{
E[] b = (E[]) new Object[a.length*2];
for(int i=0;i<a.length;i++)
{
b[i]=a[i];
}
b[a.length]=e;
a=b;
}
else
{
a[count]=e;
}
count++;
return true;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
Arrays.fill(a, null);
}
@Override
public E get(int index) {
// TODO Auto-generated method stub
if(index>=count||index<0)
{
return null;
}
else
{
return a[index];
}
}
@Override
public E set(int index, E element) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(int index, E element) {
// TODO Auto-generated method stub
if(index>=count||index<0)
{
throw new IllegalArgumentException("Index is invalid");
}
else
{
a[index]=element;
}
}
@Override
public E remove(int index) {
// TODO Auto-generated method stub
if(index>=count||index<0)
{
return null;
}
else
{
E e=a[index];
count--;
for(int i=index;i<count;i++)
{
a[i]=a[i+1];
}
return e;
}
}
@Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
E e=(E)o;
for(int i=0;i<count;i++)
{
if(a[i].equals(e))
{
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(Object o){
// TODO Auto-generated method stub
throw new UnsupportedOperationException("not yet impelented");
}
@Override
public ListIterator<E> listIterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public ListIterator<E> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
}