0

I have problems in doubly LinkedList and I am not using a tail I instead use a current pointer. When I use the tail I don't find problem but when I use a current pointer I get an error and I can not solve it. It occurs when I remove a node. My program is working but I can not remove the next node. This is the error message:

This the class DNode

public class DNode<T> { 
 T data;
 DNode<T> next;
DNode<T> prev;
public DNode(T e){
 data = e;
    next = prev = null;
}

This class DoubleLinkedList

public class DoubleLinkedList<T> {

DNode<T> head;
DNode<T> current;
int size = 0;
public DoubleLinkedList() {
    head = current = null;
}
public void Insert(T e) {
    DNode<T> tmp = new DNode(e);
    if (size == 0) {
        head = current = tmp;
    } else {
        tmp.next = current.next;
        tmp.prev = current;
        current.next = tmp;
        current = tmp;

    }
    size++;
}

public void remove() {
    if (head == current) {
        if (current.next == null) {
            head = current = null;
        } else {
            current.next.prev = null;
            head = current.next;
            current.next = null;
            current = head;
        }
    } else {
        DNode<T> tmp = current.next;

        current.prev.next = tmp;
        if (tmp != null) {
            tmp.prev = current;
        }
        current.next = current.prev = null;
        current = tmp;
    }

    size--;

}

The main calss

public static void main(String[] args) {
DoubleLinkedList<String> d = new DoubleLinkedList();


  d.Insert("jon");
    d.Insert("jack");
    d.Insert("mohammed");
    d.remove();
    d.remove();// here my problem
 }

The line with the comment is where I get an error.

Barath Sankar
  • 383
  • 4
  • 15
  • 1
    And what **is** the problem? – Ivan Sep 27 '19 at 20:32
  • when add d.remove(); second time I can not delete if find the massage error – abdullahjjj Sep 27 '19 at 20:36
  • What error message are you getting? – Jordan Sep 27 '19 at 20:43
  • Exception in thread "main" java.lang.NullPointerException at assignment1.DoubleLinkedList.remove(DoubleLinkedList.java:91)//if I click will be go to DNode tmp = current.next; at assignment1.Assignment1.main(Assignment1.java:43)// and here to d.remove(); the second one – abdullahjjj Sep 27 '19 at 20:45
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jordan Sep 27 '19 at 20:47
  • I see this before and try to solve but I can't – abdullahjjj Sep 27 '19 at 20:51

1 Answers1

0

In the else part of your remove function, you assign current to tmp. When you say current = tmp; tmp could be null. So the next time when the remove is called, if it enters the else clause(which it would in your case) current will be null, and current.next gives the null pointer exception.

EDIT : This would be a possible suggestion :

/*Deletes the last node in the list*/
public void remove() {
  if(head == null || size == 0) return; // Empty list
  if(head == current || size ==1){    // List with only one node
    head = null;
    current = null;
    size--;
    return;
  }
  DNode<T> previous = current.prev;
  previous.next = current.next;
  current.prev = null;
  current = previous;
    size--;
}
Barath Sankar
  • 383
  • 4
  • 15