JavaAlgorithms/DataStructures/Lists/CircleLinkedList.java

66 lines
2.0 KiB
Java
Raw Normal View History

package DataStructures.Lists;
public class CircleLinkedList<E> {
2020-10-24 18:23:28 +08:00
private static class Node<E> {
Node<E> next;
E value;
2020-10-24 18:23:28 +08:00
private Node(E value, Node<E> next) {
this.value = value;
this.next = next;
}
2020-10-24 18:23:28 +08:00
}
2020-10-24 18:23:28 +08:00
// 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 = null;
2020-10-24 18:23:28 +08:00
// 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;
}
2020-10-24 18:23:28 +08:00
// getter for the size... needed because size is private.
public int getSize() {
return size;
}
2020-10-24 18:23:28 +08:00
// 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) {
// we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list");
}
2020-10-24 18:23:28 +08:00
// head.next points to the last element;
head.next = new Node<E>(value, head);
size++;
}
2020-10-24 18:23:28 +08:00
public E remove(int pos) {
if (pos > size || pos < 0) {
// catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
2020-10-24 18:23:28 +08:00
// 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++) {
before = before.next;
}
Node<E> destroy = before.next;
E saved = destroy.value;
// assigning the next reference to the the element following the element we want to remove...
// the last element will be assigned to the head.
before.next = before.next.next;
// scrubbing
destroy = null;
size--;
return saved;
}
}