从链表中删除学生



我正在编写一个简单的程序来复习我的链表。我有一个问题与我的删除学生的方法。它应该做的就是检查两个学生是否有相同的学生号,如果有,则删除该学生,因为学生是唯一的。

我有一个主要的问题,那就是如果学生在列表的末尾,它会给我带来各种各样的问题。总的来说,这似乎也选错了学生。

方法如下

public boolean remove(StudentIF s) {
    // TODO Auto-generated method stub
    StudentLLNode current = head;
    if(s == null){
        return false;
    }
    if(s.getId() == (head.getStd().getId())){
        //StudentLLNode  top = head;
        head = head.getNext();
        size--;
        return true;
    }
    else{
        while(current != null){
            if(s.getId() == (current.getStd().getId())){
                current.setNext(current.getNext().getNext());
                size--;
                return true;
            }
            current = current.getNext();
        }
    }
    return false;
}

这是我的接口的存根

// remove StudentIF s *** using its equals method ***
    public boolean remove(StudentIF s);

通过做:

current.setNext(current.getNext().getNext());

似乎你正在删除下一个元素而不是当前元素。

当您到达列表末尾时,getNext()返回null。并且在null之后没有下一个元素,这就是为什么如果到达列表的末尾会得到异常的原因。

其他容器更适合避免重复元素。例如:Sets或Maps.

完整的解决方案如下:

package linkedList;
import java.util.Iterator;
public class StudentList {
    private int size = 0;
    private StudentIF head;
    public StudentList(StudentIF studentTobeAdded) {
        head = studentTobeAdded;
        size++;
    }
    public void addStudent(StudentIF studentTobeAdded) {
        StudentIF curent = head;
        while (curent.getNext() != null) {
            curent = curent.getNext();
        }
        size++;
        curent.setNext(studentTobeAdded);
    }
    public boolean removeStudent(StudentIF studentToBeRemoved)
    {
        int id = studentToBeRemoved.getId();
        StudentIF current = head;
        if (head.getId() == id) {
            head = head.getNext();
            size--;
            return true;
        }
        while (current.getNext() != null) {
            StudentIF next = current.getNext();
            if (next.getId() == id) {
                current.setNext(next.getNext());
                size--;
                return true;
            }
            current = next;
        }
        return false;
    }
    public int getSize() {
        return size;
    }
    public StudentIF getHead() {
        return head;
    }
    public void addListOfStudents(StudentIF... list) {
        for (StudentIF studentIF : list) {
            this.addStudent(studentIF);
        }
    }
    @Override
    public String toString() {
        StudentIF current = head;
        StringBuilder sb = new StringBuilder();
        while (current != null) {
            sb.append(current.getId() + " ");
            current = current.getNext();
        }
        return sb.toString();
    }
}
学生:

package linkedList;
public class StudentIF {
    private int id;
    private StudentIF next;
    public StudentIF(int id) {
        this.id = id;
        next=null;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public StudentIF getNext() {
        return next;
    }
    public void setNext(StudentIF next) {
        this.next = next;
    }

}

在while循环中,您不处理要删除的学生位于链表末尾的情况,因此current.getNext().getNext()是NPE。

另外,你的代码并没有删除id相等的学生,它实际上是删除学生后说的学生。

下面的代码应该可以解决您的问题(尽管尚未编译或测试)。

...
else {
// head == current and if we get here, the if branch has not fired
StudentLLNode previous, next;
previous = current;
current = current.getNext();
while(current != null){
   next = current.getNext();
   if(s.getId() == (current.getStd().getId())){  
        previous.setNext(next); //doesn't matter if next is null or not
        size--;
        return true;
   } 
   previous = current;
   current = next;
}
...

相关内容

  • 没有找到相关文章

最新更新