In: Computer Science
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been provided to allow you to insure that your LinkedList implementation is correct. b. Check your progress by noting the number of passing and failing tests. As you implement more of the class, more tests will pass, until all tests are passing. c. The provided tests are sufficient to show insure the LinkedList class is implemented correctly. d. You are free to add more tests as you feel are necessary; however, none of the existing tests should be modified or removed. here is the provided code class
class LinkedList<E> { private Node<E> head; // the constructor. public LinkedList(Node<E> head) { this.head = head; } /** * TODO Implement this method. * Add the given element, value, to the end of the list. * @param value The value to be added. */ public void add(E value) { } /** * TODO Implement this method. * Add the given element, value, to the list at the given index. * After this operation is complete, get(index) will return value. * After this operation, all elements that had an index * greater than index (as determined by get()) will have their index increased by one. * This operation is only valid for 0 <= index <= size(). * @param index The index at which to add value. * @param value The value to be added. * @throws IndexOutOfBoundsException when index is less than zero or greater than size. */ public void add(int index, E value) { } /** * TODO Implement this method. * Determine if the LinkedList is empty or not. * @return true if this LinkedList has no items. (This is the same as the size equal to zero.) * false if the size is greater than zero. */ public boolean isEmpty() { return false; } /** * TODO Implement this method. * Return the size (number of items) in this LinkedList. * @return the number of items in the list. */ public int size() { return -1; } /** * TODO Implement this method. * Return the element of the list at the given index. * This operation is only valid for 0 <= index < size(). * This operation does not modify the list. * @param index The index at which to retrieve an element. * @return The element of the list at the given index. * @throws IndexOutOfBoundsException when index is less than zero or greater than or equal to size. */ public E get(int index) { return null; } /** * TODO Implement this method. * Remove and return the first element (element number zero) from the list. * This operation is only valid for non-empty (size() > 0) lists. * @return The removed element. * @throws IndexOutOfBoundsException When the list is empty. */ public E remove() { return null; } /** * TODO Implement this method. * Remove and return the element with the given index from the list. * This operation is only valid for 0 <= index < size(). * After this operation, all elements that had an index * greater than index (as determined by get()) will have their index reduced by one. * @param index The index to remove an element from. * @return The removed element. * @throws IndexOutOfBoundsException when removing from index less than zero or greater than or equal to size. */ public E remove(int index) { return null; } }
//Linked list class
public class LinkedList<E> {
private Node<E> head;
private int size;
public Node<E> getHead() {
return head;
}
public void setHead(Node<E> head) {
this.head = head;
}
public LinkedList(Node<E> head) {
this.head = head;
this.size=1;
}
public LinkedList() {
this.size=0;
}
/**
* This method will add the element in the end of the
list.
* @param value
*/
public void add(E value){
Node<E> node = new
Node(value);
Node<E> prev = head;
while(prev.getNext()!=null){
prev =
(Node<E>) prev.getNext();
}
prev.setNext(node);
++size;
}
/**
* This method will insert the element at the given
index. Index value needs to be in the range of
0<=index<=size
*
* @param index
* @param value
* @throws IndexOutOfBoundsException
*/
public void add(int index, E value) throws
IndexOutOfBoundsException {
if (index < 0 || index >=
size) {
throw new
IndexOutOfBoundsException();
} else {
Node node = new
Node(value);
int count =
0;
if (index == 0)
{
node.setNext(head);
head = node;
} else {
Node prev = head;
while (true) {
if (++count == index) {
//Node tmp
= prev;
//tmp.setNext(node);
node.setNext(prev.getNext());
prev.setNext(node);
break;
}
prev = prev.getNext();
}
}
++size;
}
}
/**
* This method will check whether the linked list is
empty or not.
* @return true/false
*/
public boolean isEmpty(){
if(size>0){
return
false;
}
return true;
}
/**
* This method will return the size of linked
list.
* @return
*/
public int size(){
return this.size;
}
/**
* This method will return the element value at the
specified index.
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public E get(int index) throws
IndexOutOfBoundsException{
if(index>=0 &&
index<size){
Node temp =
head;
int count =
0;
while(true){
if(count++==index){
return (E)
temp.getValue();
}
temp = temp.getNext();
}
}else
throw new
IndexOutOfBoundsException();
}
/**
* This method will return the remove the first element
from the linked list.
* @return
* @throws IndexOutOfBoundsException
*/
public E remove() throws IndexOutOfBoundsException
{
if(size==0)
throw new
IndexOutOfBoundsException();
else{
Node temp =
head;
head =
(Node<E>) head.getNext();
--size;
return (E)
temp.getValue();
}
}
/**
* This method will return the element at the given
element.
* @param index
* @return
* @throws IndexOutOfBoundsException
*/
public E remove(int index) throws
IndexOutOfBoundsException {
if(index>=0 &&
index<size){
E value =
head.getValue();
int count =
0;
if(index==0){
head = (Node<E>) head.getNext();
--size;
return value;
}else{
Node prev = head;
while(true){
if(++count==index){
Node n =
prev.getNext();
value =
(E) n.getValue();
prev.setNext(n.getNext());
break;
}
prev = prev.getNext();
}
--size;
return value;
}
}else{
throw new
IndexOutOfBoundsException();
}
}
}
//Node class
class Node<E>{
private E value;
private Node<?> next;
public Node() { }
public Node(E value) {
super();
this.value = value;
this.next = null;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Node<?> getNext() {
return next;
}
public void setNext(Node<?> next) {
this.next = next;
}
}
//Main class for testing
public class Main {
public static void main(String[] args) {
Node<Integer> node = new
Node<Integer>(5);
LinkedList<Integer> l = new
LinkedList<Integer>(node);
l.add(7);
l.add(9);
l.add(1, 3);
System.out.println("Size:
"+l.size());
l.remove(2);
System.out.println("Size:
"+l.size());
l.remove();
System.out.println("Size:
"+l.size());
System.out.println("IS LINKEDLIST
EMPTY: "+l.isEmpty());
System.out.println("Element at
index 0: "+l.get(0));
}
}
//Output:
Size: 4
Size: 3
Size: 2
IS LINKEDLIST EMPTY: false
Element at index 0: 3