I have this class I am able to use to test my code: import java.util.Iterator; /
ID: 3769950 • Letter: I
Question
I have this class I am able to use to test my code:
import java.util.Iterator;
// This Class can be used to test the OrderedLinkedList class
public class LinkedListTest {
private class Courses implements Comparable<Courses>{
String rubric;
int number;
public Courses(String rub, int num) {
rubric = rub;
number = num;
}
public int compareTo(Courses other) {
if (rubric.compareTo(other.rubric) < 0)
return -1;
else if (rubric.compareTo(other.rubric) > 0)
return 1;
else
return number - other.number;
}
public String toString() {
return rubric + " " + number;
}
}
public static void main(String[] args) {
LinkedListTest ll = new LinkedListTest();
Courses listOfCourses[] = { ll.new Courses("COSC", 2436),
ll.new Courses("ITSE", 2409),
ll.new Courses("COSC", 1436),
ll.new Courses("ITSY", 1300),
ll.new Courses("ITSY", 1300),
ll.new Courses("COSC", 2436),
ll.new Courses("ITSE", 2417) };
OrderedLinkedList<Courses> orderedList = new OrderedLinkedList<Courses>();
for (int i = 0; i < listOfCourses.length; i++)
orderedList.findOrAdd(listOfCourses[i]);
Iterator<Courses> classIter = orderedList.iterator();
while(classIter.hasNext()) {
System.out.println(classIter.next());
} // There should be 5 courses listed in order
}
}
Here are the requirements: Implement a generic ordered linked list abstract data type. A unique feature of this generic linked list data structure is the findOrAdd method. This method searches the list for a previously added object with the same name, if one is found, the previous object is returned, if not the new object is added, in alphabetical order, to the list. Using ADTs from standard Java classes is not permitted. This is the code I have so far for the OrderedLinkedList:
import java.util.Iterator;
public class OrderedLinkedList< E extends Comparable< E>> implements Iterable <E>
{
private E first;
private E next;
private E last;
private E current;
private E temp;
private int size;
public OrderedLinkedList()
{
this.first = null;
this.next = null;
this.last = null;
this.current = null;
this.temp = null;
this.size = 0;
}
public E findOrAdd (E element)
{
E value = null;
Iterator <E> iter = this.iterator ();
if (this.first == null)
{
this.first = element;
this.current = element;
this.size++;
}
else
for (int i = 0; i < this.size; i++)
{
if (current.compareTo(element) > 0 && iter.hasNext() == false)
{
temp = this.first;
this.current = element;
this.next = temp;
this.size++;
}
else if (current.compareTo(element) < 0 && iter.hasNext() == true)
continue;
else if (current.compareTo(element) == 0)
value = element;
else if (current.compareTo(element) > 0)
{
temp = this.next;
this.current = element;
}
}
return value;
}
public Iterator <E> iterator ()
{
return new OrdListIterator <E> ();
}
private class OrdListIterator <E> implements Iterator <E>
{
private E nextNode;
public OrdListIterator ()
{
}
public boolean hasNext ()
{
return (next != null);
}
public E next ()
{
return (E) next;
}
public void remove ()
{
}
}
}
At this point, I am at a complete loss. It just keeps printing COSC 2436 infinitely.
OrderedLinkedList implements Iterable private OrdListlterator implements iterator +OrdListlteratorO +hasNextO: boolearn +nextO:E + remove0:void einte rface Iterable interface Iterator hasNext):boolean + iterator:Iterator + remove): voidExplanation / Answer
import support.BSTNode;
public class BinarySearchTree<T extends Comparable<T>> implements BSTInterface<T>
{
protected BSTNode<T> root; // reference to the root of this BST
boolean found; // used by remove
// for traversals
protected LinkUnbndQueue<T> inOrderQueue; // queue of info
protected LinkUnbndQueue<T> preOrderQueue; // queue of info
protected LinkUnbndQueue<T> postOrderQueue; // queue of info
public BinarySearchTree()
// Creates an empty BST object.
{
root = null;
}
public boolean isEmpty()
// Returns true if this BST is empty, otherwise returns false.
{
return (root == null);
}
private int recSize(BSTNode<T> tree)
// Returns the number of elements in tree.
{
if (tree == null)
return 0;
else
return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
}
public int size()
// Returns the number of elements in this BST.
{
int count = 0;
if (root != null)
{
LinkedStack<BSTNode<T>> hold = new LinkedStack<BSTNode<T>> ();
BSTNode<T> currNode;
hold.push(root);
while (!hold.isEmpty())
{
currNode = hold.top();
hold.pop();
count++;
if (currNode.getLeft() != null)
hold.push(currNode.getLeft());
if (currNode.getRight() != null)
hold.push(currNode.getRight());
}
}
return count;
}