我正在创建一个链接列表。我已经完成了大部分课程,只是无法弄清楚一些部分。链接列表,添加对象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