lundi 13 octobre 2014

Java-adding elements to list and sorting list in increasing order


Vote count:

0




I "think" my constructors and copy constructors are correct (tell me if not). The first constructor simply sets the list to null while the second constructor copies the contents of the other list parameter into another list. I am stuck on the insertSortedInOrder method, which should insert newElement in its current object list so that the increasing sorted order of the list is always kept. I "think" it is inserting elements to the list okay but I don't know how to sort the list in increasing order.



public class List<L extends Comparable<L>> implements Iterable<L> {
// you may ADD TO this inner class, but not CHANGE what's already here!
private class Node<N extends Comparable<N>> {
private N data;
private Node<N> next;
}
protected Node<L> head;
public List() {
head=null;
}
public List(List<L> other) {
this.head = other.head;
Node<L> c = other.head.next;
while(c != null){
nodeAdder(c);
c = c.next;
}
}
private Node<L> getLastNode(){
Node<L> n = head;
while (n.next != null) {
n = n.next;
} return n;
}
private void nodeAdder(Node<L> node) {
Node<L> a = new Node<T>();
a.data = node.data;
a.next = null;
getLastNode().next = a;
}
public void insertSortedInOrder(L newElement) {
Node<L> c = head;
Node<L> n = new Node<L>();
n.data = newElement;
if (c == null) {
head = n;
head.next = null;
} else {
getLastNode().next = n;
n = null;
}
}


asked 4 mins ago







Java-adding elements to list and sorting list in increasing order

Aucun commentaire:

Enregistrer un commentaire