I am working on an assignment for a programming course I am following and I am using a List to store data. The List class:
public List() {
init();
}
protected Node<E> first, current, last;
public int numberOfNodes;
public boolean isEmpty() {
return numberOfNodes == 0;
}
public List<E> init() {
numberOfNodes = 0;
first = current = last = null;
return this;
}
public int size() {
return numberOfNodes;
}
public List<E> insert(E d) {
E copy = (E)d.clone();
if (isEmpty()) {
first = current = last = new Node(copy);
numberOfNodes += 1;
return this;
}
else{
for (current = first; current != null; current = current.next){
if(current.next== null){
current.next = last = new Node(copy);
last.prior = current;
last.next = null;
numberOfNodes += 1;
return this;
}
else{
Node<E> newNode = new Node(copy);
current.next.prior = newNode;
newNode.next = current.next;
newNode.prior = current;
current.next = newNode;
current = newNode;
numberOfNodes +=1;
return this;
}
}
}
return this;
}
public E retrieve() {
return (E) current.data.clone();
}
public List<E> remove() {
if (isEmpty()){
return init();
}
else if (numberOfNodes == 1){
return init();
}
else if (current == first) {
first = current = current.next;
current.prior = null;
numberOfNodes -= 1;
}
else if (current == last) {
last = current = current.prior;
current.next = null;
numberOfNodes -= 1;
}
else {
current.prior.next = current.next;
current.next.prior = current.prior;
current = current.next;
numberOfNodes -= 1;
}
return this;
}
public boolean find(E d) {
current = first;
while((current!=null && !(d.compareTo(current.data)==0))){
current=current.next;
}
if (current==null){
return false;
}else{
return true;
}
}
public boolean setFirst() {
if(isEmpty()){
return false;
}
else{
current = first;
return true;
}
}
public boolean setLast() {
if(isEmpty()){
return false;
}
else{
current = last;
return false;
}
}
public boolean getNext() {
if(isEmpty()||current == last){
return false;
}
else{
current = current.next;
return true;
}
}
public boolean getPrior() {
if(isEmpty()||current == first){
return false;
}
else{
current = current.prior;
return true;
}
}
public List<E> clone() {
List<E> clone;
try{
clone = (List<E>)super.clone();
} catch(CloneNotSupportedException e){
throw new Error("This cannot be cloned!");
}
clone.init();
for(Node n = first; n != null; n = n.next){
clone.insert((E)n.clone().data);
}
clone.numberOfNodes = this.numberOfNodes;
return clone;
}
Now the assignment is to make the list a sorted list, sorting the items from large to small. I need to do this in a separate class called SortedList.
I made a start, but I have really no idea on what to do next:
public class SortedList extends List implements Comparable {
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
I am using the list in my program for two different objects: I use the list in my Set class. A set is basically a collection of natural numbers. For example: {1,2,3,4,5} is a set.
Furthermore, I use the list in my Table class. The table consists of Variables. A variable consists of a key and a value. The key is an identifier (Alfa for example) and the value is a Set {1,2,3}. The assignment is to order the items in the list from big to small.
So the SortedList needs to be a separate class that extends the list class! How can I do this? Many many thanks!