From c4a8e1e18c0ea36bf38c6572bdf782606e546114 Mon Sep 17 00:00:00 2001 From: Khwanchanok Srimool Date: Sun, 20 May 2018 03:37:00 +0700 Subject: [PATCH] fix null value issue stated in #436 --- DataStructures/Lists/CircleLinkedList.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/DataStructures/Lists/CircleLinkedList.java b/DataStructures/Lists/CircleLinkedList.java index 7b0d6c377bd6..d4bb2a0d2237 100644 --- a/DataStructures/Lists/CircleLinkedList.java +++ b/DataStructures/Lists/CircleLinkedList.java @@ -7,15 +7,16 @@ private Node(E value, Node 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 head; + private Node 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(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;} @@ -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(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