简体   繁体   中英

Removing student from linked list

I'm working on a simple program to brush up on my linked list. I'm having a problem with my remove student method. All it is supposed to do is check if two students have the same student id and, if they do, remove that student since students are unique.

I'm having one main problem and that is if the student is at the end of the list it's giving me all sorts of problems. It also seems to be removing the wrong student in general..

The method is as follows

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;
}

Here is the stub from my interface

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

By doing:

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

it seems like you are removing the next element instead of the current one.

When you hit the end of the list, getNext() returns null. And there is no next element after null, which is why you would get an exception if you reach the end of the list.

Other containers are better suited to avoid duplicate elements. For example, Sets or Maps.

Here is complete solution:

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();
    }
}

Student:

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;
    }



}

In your while loop, you don't handle the case where the student to be removed is at the end of the linked list and hence current.getNext().getNext() is an NPE.

Additionally your code is not removing the student where the Ids are equal, it's actually removing the student AFTER said student.

The following should fix your woes (though hasn't been compiled or tested).

...
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;
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM