meert meert - 8 days ago 6
Java Question

LinkedList || public void add(int index, E element) method ||

I need to write a Java program (class LinkedList) that should do all the stuff without using import List and I have tried but I am not sure from if they are working or not.

Can someone please help me? Especially public void add(int index, E element) method method.

Here are my classes:

package main.java.a3;

public interface List<E> {
public void add(E e);
public void add(int index, E e);
public int size();
public E get(int index);
public boolean isEmpty();
}


package main.java.a3;

public class LinkedList<E> implements List<E> {

private ListNode head;

@Override
public void add(E e) {
if(e == null) {
throw new NullPointerException("Element was null");
}
else if(head == null) {
head = new ListNode(e, null);
} else {
ListNode temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.setNext(new ListNode(e, null));
}

}

@Override
public void add(int index, E element) {

//I NEED HELP

}

@Override
public int size() {
int size = 0;
ListNode temp = head;
while(temp!= null) {
size ++;
temp = temp.getNext();
}
return size;
}

@Override
public E get(int index) {
if(index<0){
throw new IndexOutOfBoundsException("Index was negative");
}
else if(index>=size()){
throw new IndexOutOfBoundsException("Index was bigger than size");
}
ListNode temp = head;
for(int i = 0; i<index;i++) {
temp = temp.next;
}
return temp.data;
}

@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}

//innere Klasse Beginn
private class ListNode{
E data;
ListNode next;

public ListNode(E data, ListNode next) {
setData(data);
setNext(next);
}

public E getData() {
return data;
}

public void setData(E data) {
this.data = data;
}

public ListNode getNext() {
return next;
}

public void setNext(ListNode next) {
this.next = next;
}
}
//innere Klasse Ende


}

Answer
public class LinkedList<E> implements List<E> {
    private ListNode head;

    @Override
    public void add(E e) {
        if (e == null) {
            throw new NullPointerException("Element was null");
        } else if (head == null) {
            head = new ListNode(e, null);
        } else {
            ListNode temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.setNext(new ListNode(e, null));
        }

    }

    @Override
    public void add(int index, E e) {
        if (e == null) {
            throw new NullPointerException("Element was null");
        } else if (index < 0) {
            throw new IndexOutOfBoundsException("Index was negative");
        } else if (index == 0) {
            head = new ListNode(e, head);
        } else {
            ListNode temp = head;
            for (int i = 1; temp != null && i < index; ++i) {
                temp = temp.getNext();
            }
            if (temp == null) {
                throw new IndexOutOfBoundsException(
                        "Index was bigger than size");
            }
            temp.next = new ListNode(e, temp.next);
        }
    }

    @Override
    public int size() {
        int size = 0;
        ListNode temp = head;
        while (temp != null) {
            size++;
            temp = temp.getNext();
        }
        return size;
    }

    @Override
    public E get(int index) {
        if (index < 0) {
            throw new IndexOutOfBoundsException("Index was negative");
        }
        ListNode temp = head;
        for (int i = 0; temp != null && i < index; i++) {
            temp = temp.next;
        }
        if (temp == null) {
            throw new IndexOutOfBoundsException(
                    "Index was bigger than size");
        }
        return temp.data;
    }

    @Override
    public boolean isEmpty() {
        return head == null;
    }

    //innere Klasse Beginn
    private class ListNode {
        E data;
        ListNode next;

        public ListNode(E data, ListNode next) {
            setData(data);
            setNext(next);
        }

        public E getData() {
            return data;
        }

        public void setData(E data) {
            this.data = data;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }
    }
}