Skip to content

Commit

Permalink
fix null value issue stated in TheAlgorithms#436
Browse files Browse the repository at this point in the history
  • Loading branch information
Skhwan committed May 19, 2018
1 parent 6afdadd commit c4a8e1e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions DataStructures/Lists/CircleLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ private Node(E value, Node<E> next){
this.next = next;
}
}

//For better O.O design this should be private allows for better black box design
private int size;
//this will point to dummy node;
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;
public CircleLinkedList(){
//creation of the dummy node
head = new Node<E>(null,head);
size = 0;
head = new Node<>(null, head);
tail = head;
}
// getter for the size... needed because size is private.
public int getSize(){ return size;}
Expand All @@ -25,9 +26,13 @@ public void append(E value){
// we do not want to add null elements 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);
size++;}

//add new node at the end of the list and update tail node to point to new node
tail.next = new Node(value, head);
tail = tail.next;
size++;
}

public E remove(int pos){
if(pos>size || pos< 0){
//catching errors
Expand Down

0 comments on commit c4a8e1e

Please sign in to comment.