Initial revision

master
mercury 2004-02-11 08:00:27 +00:00
commit 625051a004
6 changed files with 387 additions and 0 deletions

8
ListException.java Executable file
View File

@ -0,0 +1,8 @@
public class ListException extends RuntimeException
{
public ListException(String s)
{
super(s);
} // end constructor
} // end ListException

59
ListSortedInterface.java Executable file
View File

@ -0,0 +1,59 @@
/* Andrew Coleman
CSC-2020 10:00 AM
*/
import java.util.Iterator;
public interface ListSortedInterface {
/**
* Checks to see if the list is empty and returns a boolean value. <BR>
* Preconditions: none.
* Postconditions: Returns true if empty, false if it is not empty.
*/
public boolean isEmpty();
/**
* Returns the current size of the list. <BR>
* Preconditions: none.
* Postconditions: Returns an interger of the size.
*/
public int size();
/**
* Adds a Comparable item to the list. <BR>
* Preconditions: Requires a Comparable item to insert into the list.
* Postconditions: Inserts the Comparable item in the list.
* Throws: Throws a ListException if you try and add a duplicate entry into the list.
*/
public void add(Comparable item) throws ListException;
/**
* Removes an item from the list. <BR>
* Preconditions: Requires a Comparable item to remove.
* Postconditions: Removes the item from the list.
* Throws: Throws a ListException if the item is not found.
*/
public void remove(Comparable item) throws ListException;
/**
* Returns an item in the list at the specified index. <BR>
* Preconditions: A valid integer index value.
* Postconditions: Returns a Comparable object at the said index.
* Throws: Throws a ListException if the index value is out of range.
*/
public Comparable get(int index) throws ListException;
/**
* Removes all entries in the list. <BR>
* Preconditions: None.
* Postconditions: Removes all items in the list.
*/
public void removeAll();
/**
* Creates an Iterator to cycle through the list. <BR>
* Preconditions: None.
* Postconditions: Returns the Iterator for the list.
*/
public Iterator iterator();
}

75
ListSortedIterator.java Executable file
View File

@ -0,0 +1,75 @@
/* Andrew Coleman
CSC-2020 10:00 AM
*/
import java.util.Iterator;
public class ListSortedIterator implements Iterator {
/** Index keeps the current position in the data collection and listsize the the size of the list (only used for error checking) */
private int index, listsize;
/** the collection of data */
private ListSortedInterface mylist;
/**
* Creates the Iterator for the specified list. <BR>
* Preconditions: An object that implements the ListSortedInterface.
* Postconditions: none.
*/
public ListSortedIterator (ListSortedInterface list) {
index = 1;
listsize = list.size();
mylist = list;
}
/**
* Resets all private integer values in the object to defaults, ie when the collection changes. <BR>
* Preconditions: None.
* Postconditions: Defaults all private integer values.
*/
private void reset () {
index = 1; listsize = mylist.size();
}
/**
* Determines if there are more objects in the collection. <BR>
* Preconditions: None.
* Postconditions: Returns a boolean value true if there are more objects, false otherwise.
* Throws: Throws a ListException if the data has been changed.
*/
public boolean hasNext () {
if (listsize != mylist.size()) {
reset();
throw new ListException ("Changes have been made to the collection.");
}
return (index <= listsize);
}
/**
* Returns an Object at the current position in the collection. <BR>
* Preconditions: None.
* Postconditions: Returns the current Object in the collection.
* Throws: Throws a ListException if the data has been changed, or if the end of the list has been reached.
*/
public Object next () {
if (listsize != mylist.size()) {
reset ();
throw new ListException ("Changes have been made to the collection.");
}
if (index <= listsize) {
index++;
return ((Object) mylist.get (index - 1));
}
else
throw new ListException ("End of collection has been reached.");
}
/**
* Removes the current item in the list. <BR>
* Preconditions: None.
* Postconditions: Removes the current item from the list.
*/
public void remove () {
mylist.remove (mylist.get (index));
}
}

187
ListSortedReferenceBased.java Executable file
View File

@ -0,0 +1,187 @@
/* Andrew Coleman
CSC-2020 10:00 AM
*/
import java.util.Iterator;
public class ListSortedReferenceBased implements ListSortedInterface {
/** The Node that points to the first item in the collection */
private Node head;
/** Keeps track of the current size of the collection */
private int size;
/**
* Sets up the instance variables to default values. <BR>
* Preconditions: None.
* Postconditions: Sets the default values for the instance variables.
*/
public ListSortedReferenceBased () {
head = null;
size = 0;
}
public boolean isEmpty () {
return (size == 0);
}
public int size () {
return size;
}
public void removeAll () {
head = null; size = 0;
}
public Iterator iterator () {
return new ListSortedIterator (this);
}
/**
* Finds the Node of a specified Comparable item. <BR>
* Preconditions: Requires a Comparable object.
* Postconditions: Returns a Node of the item, null if it is not found.
*/
private Node find (Comparable item) {
Node curr = head;
while (curr != null) {
if ( ((Comparable) curr.getItem()).compareTo(item) == 0)
return curr;
curr = curr.getNext();
}
return null;
}
/**
* Finds the Node at the specified index in the collection. <BR>
* Preconditions: Assumes an integer in the valid range (1 - size()).
* Postconditions: Returns the node at the index.
*/
private Node find (int index) {
Node curr = head;
for (int skip = 1; skip < index; skip++)
curr = curr.getNext();
return curr;
}
/**
* Locates the last Node that is less than the passed Comparable object. <BR>
* Preconditions: Requires a Comparable object, and assumes that size > 1.
* Postconditions: Returns a Node directly before where the passed object should be in the collection, or null if it should be the first item.
*/
private Node findplace (Comparable item) {
/* curr will compare itself to item until it is too big, then prev is in the correct place */
Node curr = head.getNext(), prev = head;
/* must check to see if the first item is the item searched for */
if (((Comparable) prev.getItem()).compareTo(item) > 0)
return null;
/* while not at the end of the list and the item in curr is less than item */
while (curr != null && ((Comparable) curr.getItem()).compareTo(item) < 0) {
prev = curr;
curr = curr.getNext();
}
return prev;
}
public void add (Comparable item) throws ListException {
/* is item in the collection? */
if (find (item) == null) {
/* curr will be the active Node in the collection */
Node curr = head;
/* temp will be the inserted Node */
Node temp;
/* if the list is empty, just put in temp */
if (size == 0)
head = new Node (item);
/* if the size of the collection is 1, then check to see if the Node is bigger or less */
else if (size == 1) {
if ( ((Comparable) curr.getItem()).compareTo (item) > 0) {
temp = new Node (item, head);
head = temp;
}
else {
temp = new Node (item);
head.setNext(temp);
}
}
/* well, the collection must be > 1 so now use the nifty findplace and figure out the Node that temp should be placed after */
else {
curr = findplace (item);
if (curr == null)
temp = new Node (item, head);
else {
temp = new Node (item, curr.getNext());
curr.setNext (temp);
}
}
/* increment the all important size */
size++;
}
else {
/* use a variable and one throw rather than two different throws... less typing i suppose */
String s;
if (size == 0)
s = " or list is empty";
else
s = "";
throw new ListException ("Item is already in data collection" + s + ".");
}
}
public void remove (Comparable item) throws ListException {
/* if the item is in fact in the collection */
if (find (item) == null)
throw new ListException ("Item is not in the data collection.");
else {
/* if there is more than 2 objects in the collection then find the previous Node */
if (size > 2) {
Node curr = findplace(item);
/* just skip over the item to remove */
curr.setNext (curr.getNext().getNext());
}
/* if the size is 2, then one or the other is the item in question */
else if (size == 2) {
/* must cast... Node and Object only */
if (((Comparable) head.getItem()).compareTo(item) == 0)
head = head.getNext();
else
head.setNext(null);
}
/* if the size is 1, well, just remove it all */
else
head = null;
/* and they will all remove exactly 1, so 1 decrement for all cases */
size--;
}
}
public Comparable get (int index) throws ListException {
/* must check and see if index is within the range of the collection */
if (index > 0 && index <= size) {
Node curr = head;
for (int skip = 1; skip < index; skip++)
curr = curr.getNext ();
/* must cast to Comparable since Node uses Object(s) */
return ((Comparable) curr.getItem());
}
else
throw new ListException ("Index is out of range.");
}
/**
* Returns a string containing each Node separated by newlines. <BR>
* Preconditions: None.
* Postconditions: Returns a string containing an easy to read format of the collection.
*/
public String toString () {
String result = "";
/* i save 1 line of code if i use the iterator, rather than looping through the list */
/* besides, i haven't used the iterator anywhere else */
Iterator allyourbase = iterator();
while (allyourbase.hasNext ())
result += allyourbase.next() + "\n";
return result;
}
}

30
Node.java Executable file
View File

@ -0,0 +1,30 @@
public class Node {
private Object item;
private Node next;
public Node (Object item) {
this.item = item;
next = null;
}
public Node (Object item, Node next) {
this.item = item;
this.next = next;
}
public void setItem (Object item) {
this.item = item;
}
public void setNext (Node next) {
this.next = next;
}
public Object getItem () {
return item;
}
public Node getNext () {
return next;
}
}

28
drivertest.java Executable file
View File

@ -0,0 +1,28 @@
/* Andrew Coleman
CSC-2020 10:00 AM
*/
public class drivertest {
public static void main (String[] args) {
ListSortedInterface list = new ListSortedReferenceBased();
try {
list.add("b");
System.out.print(list + "\n\n");
list.add("a");
System.out.print(list + "\n\n");
list.add("d");
System.out.print(list + "\n\n");
list.add("c");
System.out.print(list + "\n\n");
list.remove("a");
System.out.print(list + "\n\n");
list.remove("d");
System.out.print(list + "\n\n");
System.out.println(list.get(2));
}
catch (ListException e) {
System.out.println(e);
}
}
}