Merge pull request #278 from teerapat1739/patch-1

Coding standards
This commit is contained in:
Varun Upadhyay 2017-10-27 10:10:32 -07:00 committed by GitHub
commit e43897289b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,32 +7,44 @@ public class CircleLinkedList<E>{
this.next = next;
}
}
private int size; //For better O.O design this should be private allows for better black box design
private Node<E> head; //this will point to dummy node;
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;
head = new Node<E>(null,head); //creation of the dummy node
//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;
//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;
}
public int getSize(){ return size;} // getter for the size... needed because size is private.
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.
// getter for the size... needed because size is private.
public int getSize(){ return size;}
// 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.
public void append(E value){
if(value == null){
throw new NullPointerException("Cannot add null element to the list"); // 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");
}
head.next = new Node<E>(value,head); //head.next points to the last element;
//head.next points to the last element;
head.next = new Node<E>(value,head);
size++;}
public E remove(int pos){
if(pos>size || pos< 0){
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); //catching errors
//catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
Node<E> iterator = head.next;
Node<E> before = head; //we need to keep track of the element before the element we want to remove we can see why bellow.
//we need to keep track of the element before the element we want to remove we can see why bellow.
Node<E> before = head;
for(int i = 1; i<=pos; i++){
iterator = iterator.next;
before = before.next;
}
E saved = iterator.value;
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.
iterator.next = null; // scrubbing
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
before.next = iterator.next;
// scrubbing
iterator.next = null;
iterator.value = null;
return saved;