I have a project where i am having to create a doubly link list of persons, with lastname, firstname, and id number.
Basically, how do I implement my compareTo method in my add method of dbl list ???
thanks in advance.
Here is my person class:
public class Person implements Comparable<Person>{
String firstName;
String lastName;
int idNum;
//getters and setters for Person
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getIdNum() {
return idNum;
}
public void setIdNum(int idNum) {
this.idNum = idNum;
}
//constructor for Person
public Person(String firstName, int idNum, String lastName) {
super();
this.firstName = firstName;
this.idNum = idNum;
this.lastName = lastName;
}
//compareTo method for object Person
public int compareTo(Person o) {
if(this.idNum < o.getIdNum()){
return -1;
} else if (this.idNum == o.getIdNum()){
return 0;
} else
return 1;
}
}
Here is my Node class:
class Node<E> {
Node(E value) {
this.value = value;
}
Node(E value, Node<E> prev, Node<E> next) {
this.value = value;
setPrev(prev);
setNext(next);
}
void setPrev(Node<E> prev) {
this.prev = prev;
}
void setNext(Node<E> next) {
this.next = next;
}
Node<E> getPrev() {
return prev;
}
Node<E> getNext() {
return next;
}
E getValue() {
return value;
}
private E value;
private Node<E> prev;
private Node<E> next;
}
And finally, here is my SortedDblList class
public class SortedDblList <E extends Comparable <? super E>> {
private Node<E> head = new Node<E>(null);
private Node<E> tail = new Node<E>(null);
private int size = 0;
public Node<E> get(int index) throws IndexOutOfBoundsException {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
} else {
Node<E> cursor = head;
for (int i = 0; i < index; i++) {
cursor = cursor.getNext();
}
return cursor;
}
}
public boolean add(E element){
//if isEmpty == true, then head == element
//if head = NULL add there.
Node<E> cursor = head;
Node<E> temp = new Node<E>(element);
for (int i = 0; i < size; i++) {
cursor = cursor.getNext();
//((Comparable<? super E>) cursor).compareTo(temp);
cursor.compareTo(element); //error
/*NEED HELP HERE TO COMPARE*/
}
/*temp.setPrev(cursor);
temp.setNext(cursor.getNext());
cursor.getNext().setPrev(temp);
cursor.setNext(temp);
size++;*/
}
}
I might have some more questions here so thanks in advance for anyone willing to help.