Skip to content

Commit e438972

Browse files
authored
Merge pull request TheAlgorithms#278 from teerapat1739/patch-1
Coding standards
2 parents 9fd0548 + 894ec55 commit e438972

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

data_structures/Lists/CircleLinkedList.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,44 @@ private Node(E value, Node<E> next){
77
this.next = next;
88
}
99
}
10-
private int size; //For better O.O design this should be private allows for better black box design
11-
private Node<E> head; //this will point to dummy node;
12-
public CircleLinkedList(){ //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;
13-
head = new Node<E>(null,head); //creation of the dummy node
10+
//For better O.O design this should be private allows for better black box design
11+
private int size;
12+
//this will point to dummy node;
13+
private Node<E> head;
14+
//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;
15+
public CircleLinkedList(){
16+
//creation of the dummy node
17+
head = new Node<E>(null,head);
1418
size = 0;
1519
}
16-
public int getSize(){ return size;} // getter for the size... needed because size is private.
17-
public void append(E value){ // for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
20+
// getter for the size... needed because size is private.
21+
public int getSize(){ return size;}
22+
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
23+
public void append(E value){
1824
if(value == null){
19-
throw new NullPointerException("Cannot add null element to the list"); // we do not want to add null elements to the list.
25+
// we do not want to add null elements to the list.
26+
throw new NullPointerException("Cannot add null element to the list");
2027
}
21-
head.next = new Node<E>(value,head); //head.next points to the last element;
28+
//head.next points to the last element;
29+
head.next = new Node<E>(value,head);
2230
size++;}
2331
public E remove(int pos){
2432
if(pos>size || pos< 0){
25-
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); //catching errors
33+
//catching errors
34+
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
2635
}
2736
Node<E> iterator = head.next;
28-
Node<E> before = head; //we need to keep track of the element before the element we want to remove we can see why bellow.
37+
//we need to keep track of the element before the element we want to remove we can see why bellow.
38+
Node<E> before = head;
2939
for(int i = 1; i<=pos; i++){
3040
iterator = iterator.next;
3141
before = before.next;
3242
}
3343
E saved = iterator.value;
34-
before.next = iterator.next; // assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
35-
iterator.next = null; // scrubbing
44+
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
45+
before.next = iterator.next;
46+
// scrubbing
47+
iterator.next = null;
3648
iterator.value = null;
3749
return saved;
3850

0 commit comments

Comments
 (0)