내 목록 끝에 요소를 추가하는 데 문제가 있습니다. 목록의 시작 부분에 계속 추가됩니다. 나는 잠시 동안 이것에 있었고, 단지 붙어서 길을 잃었다. 여기 노드 끝에 새 요소 추가 (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());
당신이 조금하고있는 일을 반성하십시오. 새로운 노드를 만들고, 새로운 노드의 링크를 이전 목록으로 설정하고 "목록의 첫 번째 노드"를 새로운 노드. 그러면 노드가 목록에 첫 번째로 놓입니다. –