У меня возникли проблемы с добавлением элементов в конец моего списка. Он продолжает добавлять в начало списка. Я вот уже какое-то время, просто застрял и потерял.Добавление нового элемента в конец узла (Java)
public class RefUnsortedList<T> implements ListInterface<T> {
protected int numElements; // number of elements in this list
protected LLNode<T> currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, else false
protected LLNode<T> location; // node containing element, if found
protected LLNode<T> previous; // node preceeding location
protected LLNode<T> list; // first node on the list
public RefUnsortedList() {
numElements = 0;
list = null;
currentPos = null;
}
public void add(T element) {
// Adds element to this list.
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
Вот мой основной класс:
RefUnsortedList<Patient> patient1 = new RefUnsortedList<Patient>();
Patient entry;
entry = new Patient("Tam Ngo", "0848896");
patient1.add(entry);
entry = new Patient("Mike You", "0848896");
patient1.add(entry);
System.out.println(patient1.toString());
Подумайте, что вы делаете немного ... вы создаете новый узел, вы устанавливаете ссылку (следующий) нового узла в старый список и устанавливаете «первый узел в списке» на новый узел. Это поместит узел первым в список. –