I was trying to learn to implement Doubly Linked List in java just as an exercise.
but I am stuck at remove() method.
The contents of the list are,
1 2 3 4
When I try to remove the element which is not present.It shows the NullPointerException at line no. 21 instead of printing not found.
The other codes that I saw was a little complicated and different from what I am doing. The method is,
void remove(int data){
Node n = head;
if(head==null)
System.out.println("Not Found");
else if(head.d==data){
if(head==tail){
head=null;
tail=null;
}
else{
head=head.next;
head.prev=null;
}
}
else{
while(n!=null && n.d==data){
n=n.next;
}
if(n==tail){
tail=tail.prev;
tail.next=null;
}
else if(n!=null){
n.prev.next=n.next;
n.next.prev=n.prev;
}
if(n==null)
System.out.println("Not Found");
}
}
So my Question is, Am I doing it completely wrong? OR what is the problem? Pardon me if it's just too foolish.
NullPointerExceptionfor every input or just for some inputs? What is the content of the list and what is the input when it gives you the exception?