refactor indentation

This commit is contained in:
Khwanchanok Srimool 2018-05-20 03:25:30 +07:00
parent 52f7e4e97e
commit 6afdadd28f

View File

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