Now has the add method and the iterator:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class OurLinkedList<E> implements OurList<E> {
private Node first = null;
private int size = 0;
@Override
public boolean isEmpty() {
return (first == null);
}
@Override
public int size() {
return size;
}
@Override
public void add(int index, E item) throws ListIndexOutOfBoundsException {
if (index < 0 || index > size) {
throw new ListIndexOutOfBoundsException("Index " + index + " is out of bounds");
}
if (index == 0) {
first = new Node(item, first);
size++;
} else {
Node current = first;
for (int i = 0; i < index - 1; ++i) {
current = current.next;
}
Node newNode = new Node(item, current.next);
current.next = newNode;
size++;
}
}
@Override
public E get(int index) throws ListIndexOutOfBoundsException {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove(int index) throws ListIndexOutOfBoundsException {
// TODO Auto-generated method stub
}
@Override
public void clear() {
first = null;
size = 0;
}
private class Node {
E item;
Node next;
Node(E item, Node next) {
this.item = item;
this.next = next;
}
}
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new LinkedListIterator();
}
private class LinkedListIterator implements Iterator<E>{
Node current = first;
public boolean hasNext() {
return (current != null);
}
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E item = current.item;
current = current.next;
return item;
}
/**
* method not supported
**/
public void remove() {
throw new UnsupportedOperationException();
}
}
}
OurList interface that OurLinkedList implements:
/**
OurList interface is modeled after the List interface in the
Java Collections Framework. The prefix "Our" is to avoid name
clashes with the standard Java interfaces and classes.
Author: Elena Machkasova
Purpose: to be used in CSci 2101 UMM course
**/
public interface OurList<E> extends Iterable<E> {
/**
* @return true if this list contains no elements,
* false otherwise
*/
public boolean isEmpty();
/**
* Returns the number of elements in this list.
* @return the number of elements in this list
*/
public int size();
/**
* Inserts the specified element at the specified index in this list
* @param index - the index where to insert the element
* @param item - the item to be inserted
* @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()).
*/
public void add(int index, E item) throws ListIndexOutOfBoundsException;
/**
* Returns the element at the specified position in this list.
* @param index - the index of the element to be returned
* @return - the element at the specified position in this list.
* @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
*/
public E get(int index) throws ListIndexOutOfBoundsException;
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their indices).
* @param index - the index of the element to removed.
* @throws ListIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
*/
public void remove(int index) throws ListIndexOutOfBoundsException;
/**
* Removes all of the elements from this list (optional operation).
* This list will be empty after this call returns.
* @return
*/
public void clear();
}
ListIndexOutOfBoundsException
public class ListIndexOutOfBoundsException extends Exception {
/**
Constructor that sets the message
**/
public ListIndexOutOfBoundsException(String message) {
// passing the message to the constructor of the superclass
super(message);
}
}
Testing code for OurLinkedList (we don't have the class itself yet!)
/**
The class tests methods of a linked list OurLinkedList via its
interface OurList
**/
public class TestOurLinkedList {
public static void main(String [] args) throws ListIndexOutOfBoundsException {
OurList<String> strings = new OurLinkedList<String>();
// the list should be empty initially
System.out.println("A newly-created list:");
// expected: true
System.out.println("List isEmpty is " + strings.isEmpty());
//expected: The size of the list is 0
System.out.println("The size of the list is " + strings.size());
strings.add(0,"hello");
// the list after adding an element
System.out.println("A list with one element:");
// expected: false
System.out.println("List isEmpty is "+ strings.isEmpty());
//expected: The size of the list is 1
System.out.println("The size of the list is " + strings.size());
strings.add(1,"bye");
strings.add(2,"greetings!");
// the list after adding 3 elements
System.out.println("A list with three elements:");
// expected: false
System.out.println("List isEmpty is "+ strings.isEmpty());
//expected: The size of the list is 3
System.out.println("The size of the list is " + strings.size());
// testing add and get methods
System.out.println("After adding three strings the list is");
for(int i = 0; i < strings.size(); ++i) {
String s = strings.get(i);
System.out.println(s);
}
//testing an iterator
System.out.println("Iterator with the three strings:");
for (String s : strings) {
System.out.println(s);
}
// test exception
try {
strings.get(55);
} catch (ListIndexOutOfBoundsException e) {
System.out.println(e);
}
//much more testing is needed!
}
}