我正在創建一個鏈接列表。我已經完成了大部分課程,只是無法弄清楚一些部分。鏈接列表,添加對象im正確的排序位置
我試過不同的代碼,但我不知道什麼是正確的,以及如何去做。
任何人都可以請幫助我。
public class LinkedList<T> implements LinkedListADT<T> {
private int count; // the current number of elements in the list
private LinearNode<T> list; // pointer to the first element
private LinearNode<T> last; // pointer to the last element
/*
* Create an empty list first
*/
public LinkedList() {
this.count = 0;
this.last = null;
this.list = null;
}
// 1. add to end of list
public void add(T element) {
LinearNode<T> node = new LinearNode<T>(element);
if (size() == 0) {
this.last = node; // This is the last and the
this.list = node; // first node
this.count++;
} // end if
else if (!(contains(element))) {
last.setNext(node); // add node to the end of the list
last = node; // now make this the new last node.
count++;
} // end if
}
}
如何在列表中正確的排序位置添加對象。這是我有,但無法弄清楚正確的代碼。
/*
* 2. add in correct sorted position
*/
public void addSorted(T element) {
LinearNode<T> node = new LinearNode<T>(element);
}
我已經見過這種方式,但它不是我在做什麼,並且不能在此工作 – 2014-10-31 03:47:28