package Examples;
import java.util.*;
/**
* A simple class implementing a sorted list. Internally it also maintains
* a pointer to the greatest element in the list (if it is not
* empty).
*
*
There are some faults in the method {@link #insert insert}.
*/
public class SimpleSortedList {
private LinkedList s;
private Comparable max;
/**
* This constructs an empty list.
*/
public SimpleSortedList() {
s = new LinkedList();
}
/**
* This inserts a new element into the list.
*/
public void insert(Comparable x) {
//System.out.println("** insert") ;
int i = 0;
for (Comparable y : s) {
if (y.compareTo(x) > 0) {
break;
}
i++;
}
s.add(i, x);
// deliberate error:
if (max == null || x.compareTo(max) < 0) {
max = x;
}
}
/**
* Return and remove the greatest element from the list, if it is
* not empty.
*/
public Comparable get() {
//System.out.println("** get") ;
Comparable x = max;
s.remove(max);
if (s.isEmpty()) {
max = null;
} else {
max = s.getLast();
}
return x;
}
}