import java.util.NoSuchElementException; public class LinkedList> { private static class Node { private E value; private Node next; private Node(E value, Node next) { this.value = value; this.next = next; } } private Node head = null; private int size; public void addFirst(E element) { if (element == null) { throw new NullPointerException(); } head = new Node(element, head); size++; } public int size() { return size; } public int sizeRec() { return sizeRec(head); } private int sizeRec(Node current) { if (current == null) { return 0; } return 1 + sizeRec(current.next); } public E findMax() { if (head == null) { throw new NoSuchElementException(); } return findMax(head); } private E findMax(Node current) { if (current.next == null) { return current.value; } E result = findMax(current.next); if (result.compareTo(current.value) > 0) { return result; } else { return current.value; } } public E get(int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } return get(head, index); } private E get(Node current, int index) { if (current == null) { throw new IndexOutOfBoundsException(); } if (index == 0) { return current.value; } return get(current.next, index-1); } public int indexOfLast(E element) { if (element == null) { throw new NullPointerException(); } return indexOfLast(head, element); } private int indexOfLast(Node current, E element) { if (current == null) { // cas de base return -1; } if (current.value.equals(element)) { // cas de base return 0; } int result = indexOfLast(current.next, element); if (result == -1) { return result; } return result + 1; } public int indexOf(E element) { if (element == null) { throw new NullPointerException(); } return indexOf(head, element); } private int indexOf(Node current, E element) { if (current == null) { return -1; } if (current.value.equals(element)) { return 0; } int result = indexOf(current.next, element); if (result > -1) { return 1+result; } return -1; } public static void main(String[] args) { LinkedList l = new LinkedList(); for (int i=0; i<5; i++) { l.addFirst(i); } System.out.println("length = " + l.sizeRec()); } }