fix null value issue stated in #436

This commit is contained in:
Khwanchanok Srimool
2018-05-20 03:37:00 +07:00
parent 6afdadd28f
commit c4a8e1e18c

View File

@ -7,15 +7,16 @@ public class CircleLinkedList<E>{
this.next = next; this.next = next;
} }
} }
//For better O.O design this should be private allows for better black box design //For better O.O design this should be private allows for better black box design
private int size; private int size;
//this will point to dummy node; //this will point to dummy node;
private Node<E> head; private Node<E> head;
private Node<E> tail;
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty; //constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
public CircleLinkedList(){ public CircleLinkedList(){
//creation of the dummy node head = new Node<>(null, head);
head = new Node<E>(null,head); tail = head;
size = 0;
} }
// getter for the size... needed because size is private. // getter for the size... needed because size is private.
public int getSize(){ return size;} public int getSize(){ return size;}
@ -25,9 +26,13 @@ public class CircleLinkedList<E>{
// we do not want to add null elements to the list. // we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list"); throw new NullPointerException("Cannot add null element to the list");
} }
//head.next points to the last element;
head.next = new Node<E>(value,head); //add new node at the end of the list and update tail node to point to new node
size++;} tail.next = new Node(value, head);
tail = tail.next;
size++;
}
public E remove(int pos){ public E remove(int pos){
if(pos>size || pos< 0){ if(pos>size || pos< 0){
//catching errors //catching errors