Skip to content

Commit c4a8e1e

Browse files
committed
fix null value issue stated in TheAlgorithms#436
1 parent 6afdadd commit c4a8e1e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

DataStructures/Lists/CircleLinkedList.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ private Node(E value, Node<E> next){
77
this.next = next;
88
}
99
}
10+
1011
//For better O.O design this should be private allows for better black box design
1112
private int size;
1213
//this will point to dummy node;
1314
private Node<E> head;
15+
private Node<E> tail;
1416
//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;
1517
public CircleLinkedList(){
16-
//creation of the dummy node
17-
head = new Node<E>(null,head);
18-
size = 0;
18+
head = new Node<>(null, head);
19+
tail = head;
1920
}
2021
// getter for the size... needed because size is private.
2122
public int getSize(){ return size;}
@@ -25,9 +26,13 @@ public void append(E value){
2526
// we do not want to add null elements to the list.
2627
throw new NullPointerException("Cannot add null element to the list");
2728
}
28-
//head.next points to the last element;
29-
head.next = new Node<E>(value,head);
30-
size++;}
29+
30+
//add new node at the end of the list and update tail node to point to new node
31+
tail.next = new Node(value, head);
32+
tail = tail.next;
33+
size++;
34+
}
35+
3136
public E remove(int pos){
3237
if(pos>size || pos< 0){
3338
//catching errors

0 commit comments

Comments
 (0)