LinkedList - delete(Object) method works strange - deleting last element doesn't work properly

StackOverflow https://stackoverflow.com/questions/22662494

Frage

I have LinkedList with test program. As you can see in that program I add some Students to the list. I can delete them. If I choose s1,s2,s3 oraz s4 to delete everything runs well, and my list is printed properly and information about number of elements is proper. But if I delete last element (in this situation - s5) info about number of elements is still correct, but this element is still printed. Why is that so? Where is my mistake?

public class Lista implements List {
private Element head = new Element(null); //wartownik
private int size; 
public Lista(){
    clear();
}

public void clear(){
    head.setNext(null);
    size=0;
}

public void add(Object value){
    if (head.getNext()==null) head.setNext(new Element(value)); 
    else {
        Element last = head.getNext();
        //wyszukiwanie ostatniego elementu
    while(last.getNext() != null)
        last=last.getNext();
    // i ustawianie jego referencji next na nowowstawiany Element
    last.setNext(new Element(value));}
    ++size;
}


 public Object get(int index) throws IndexOutOfBoundsException{
    if(index<0 || index>size) throw new IndexOutOfBoundsException();
    Element particular = head.getNext();
    for(int i=0; i <= index; i++)
        particular = particular.getNext();
    return particular.getValue();
}  

public boolean delete(Object o){
    if(head.getNext() == null) return false;
    if(head.getNext().getValue().equals(o)){
        head.setNext(head.getNext().getNext());
        size--;
        return true;
    }

    Element delete = head.getNext();
    while(delete != null && delete.getNext() != null){
        if(delete.getNext().getValue().equals(o)){
            delete.setNext(delete.getNext().getNext());
                            size--;
            return true;
        }
        delete = delete.getNext();
    }
    return false;
}

public int size(){
    return size;
}

public boolean isEmpty(){
    return size == 0;
}

public IteratorListowy iterator() {
    return new IteratorListowy();
}

public void wyswietlListe() {
    IteratorListowy iterator = iterator();
    for (iterator.first(); !iterator.isDone(); iterator.next())
    {
        System.out.println(iterator.current());
    }
    System.out.println();
}

public void infoOStanie() {
    if (isEmpty()) {
      System.out.println("Lista pusta.");
  }
  else
  {
      System.out.println("Lista zawiera " + size() + " elementow.");
  }

}

private static final class Element{
    private Object value; 
    private Element next; //Referencja do kolejnego obiektu

    public Element(Object value){ 
        setValue(value); 
      }

    public void setValue(Object value) {
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    //ustawia referencję this.next na obiekt next podany w atgumencie
    public void setNext(Element next) {
        if (next != null)
        this.next = next;
    }

    public Element getNext(){
        return next;
    }

}

private class IteratorListowy implements Iterator{
private Element current;

 public IteratorListowy() {
   current = head;
} 

public void next() {
   current = current.next;
}   
public boolean isDone() {
   return current == null;
} 
 public Object current() {
   return current.value;
}

 public void first() {
   current = head.getNext();
}
}
}

test

public class Program {

public static void main(String[] args) {
  Lista lista = new Lista();
  Iterator iterator = lista.iterator();
  Student s1 = new Student("Kowalski", 3523);
  Student s2 = new Student("Polański", 45612);
  Student s3 = new Student("Karzeł", 8795);
  Student s4 = new Student("Pałka", 3218);
  Student s5 = new Student("Konowałek", 8432);
  Student s6 = new Student("Kłopotek", 6743);
  Student s7 = new Student("Ciołek", 14124);
  lista.add(s1);
  lista.add(s2);
  lista.add(s3);
  lista.add(s4);
  lista.add(s5); 
  lista.wyswietlListe();
  lista.delete(s5);
  lista.wyswietlListe();


  lista.infoOStanie();

  lista.clear();
  lista.infoOStanie();

}
}
War es hilfreich?

Lösung

The problem is that your setNext(Element next) method does not set anything if next == null. And that is the case for the last element of your list.

So when you call delete.setNext(delete.getNext().getNext());, nothing is actually set because delete.getNext().getNext() is null!

Remove the if (next != null) condition in setNext and it will work.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top