0
我必須編寫一個程序來實現學生的鏈接列表,在這種情況下,Student是一個將被調用的單獨的類。我的鏈接列表應該能夠找到學生使用該名學生的名稱,這是我的程序失敗的地方,它似乎沒有找到該學生的節點,因此它也不會刪除。我正在爲一個類寫這篇文章,教授不允許使用內置的鏈接列表類。在java中查找並從這個鏈表中刪除一個節點
這是我的代碼
public class StudentList {
StudentNode head;
public StudentList() {
shead = null;
}
public void setShead(StudentNode data) {
shead = data;
}
public StudentNode getShead() {
return shead;
}
public void insert(StudentNode data) {
StudentNode iterator = head;
if (iterator == null) {
head = data;
} else {
while (iterator.getnext() != null) {
iterator = iterator.getnext();
}
iterator.setnext(data);
}
}
public void delete(StudentNode data) {
StudentNode iterator = head;
StudentNode current;
if (iterator == data) {
head = iterator.getnext();
} else {
while (iterator.getnext() != null) {
if (iterator.getnext().equals(data)) {
// current
iterator.setnext(iterator.getnext().getnext());
// current.setSptr(iterator.getnext());
break;
} else {
iterator = iterator.getSptr();
}
}
}
}
public StudentNode find(String n) {
StudentNode iterator = head;
StudentNode result = null;
while (iterator.getnext() != null) {
if (iterator.getStudent().getName().equals(n)) {
result = iterator;
System.out.println("" + iterator.getStudent().getName());
} else {
result = null;
}
iterator = iterator.getnext();
}
return result;
}
public void print(){
StudentNode iterator = head;
while(iterator != null){
JOptionPane.showMessageDialog(null,""+ iterator.getStudent().getName());
iterator = iterator.getnext();
}
}
}
這是節點
public class StudentNode{
private Student data;
private StudentNode next;
public StudentNode(){
data = null;
next = null;
}
public StudentNode(Student data){
this.data = data;
next = null;
}
public Student getStudent(){
return data;
}
public void setStudent(Student data){
this.data = data;
}
public StudentNode getnext(){
return next;
}
public void setnext(StudentNode next){
this.next = next;
}
}