Problem:
Implement an ordered list using LinkedList and ListIterator (the one in the book). You need to provide the following methods: AddSorted, Retrieve, Delete and Search.
Output:
Not available.
Solution:
/**------------------MyComparable.java---------------------*/
public interface MyComparable
{
public int MyCompareTo(Object O);
}
/**------------------OrderedList.java---------------------*/
import java.util.*;
public class OrderedList<E extends MyComparable>
{
private LinkedList<E> linkedList = new LinkedList<E>();
private ListIterator<E> listIterator;
public void AddSorted(E entry)
{
listIterator = linkedList.listIterator();
while(listIterator.hasNext())
{
if(entry.MyCompareTo(listIterator.next())>0)
{
listIterator.previous();
listIterator.add(entry);
return;
}
}
listIterator.add(entry);
}
public void Delete(E entry)
{
listIterator=linkedList.listIterator();
while(listIterator.hasNext())
{
if(entry.MyCompareTo(listIterator.next())==0)
{
listIterator.remove();
System.out.println("Deleted");
return;
}
}
}
public E Retrieve(E entry)
{
listIterator = linkedList.listIterator();
while(listIterator.hasNext())
{
if( entry.MyCompareTo(listIterator.next()) == 0)
{
return listIterator.previous();
}
}
throw new NoSuchElementException();
}
public void Search(E entry)
{
listIterator = linkedList.listIterator();
while(listIterator.hasNext())
{
if(entry.MyCompareTo(listIterator.next())==0)
{
System.out.println("Found");
return;
}
}
System.out.println("Not Found");
}
}
/**----------------------Students.java-------------------------*/
public class Student implements MyComparable
{
private String name;
private int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int MyCompareTo(Object o)
{
Student student =(Student) o;
if(id < student.getId())
return -1;
else if(id > student.getId())
return 1;
else
return 0;
}
public String toString() {
return "Student, " + name + "\n" + "Id, " + id;
}
}
/**-------------------------Tester.java------------------------------*/
public class Tester
{
public static void main(String[] args)
{
OrderedList<Student> student = new OrderedList<Student>();
student.AddSorted(new Student("name1",1));
student.AddSorted(new Student("name2",2));
student.AddSorted(new Student("name3",3));
student.AddSorted(new Student("name4",4));
student.AddSorted(new Student("name5",5));
System.out.println("Searching for \"name1\": ");
//Searching based on id number not name
student.Search(new Student("name1",1));
System.out.println();
System.out.println("Retrieving \"name1\": ");
System.out.println(student.Retrieve(new Student("name1",1)));
System.out.println();
System.out.println("Deleting \"name1\": ");
student.Delete(new Student("name1",1));
System.out.println();
System.out.println("Re-searching for \"name1\": ");
student.Search(new Student("name1",1));
System.out.println();
}
}