Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/java/generics_interfaces.txt

1.8 KiB, 1 run

created by r1870400018:11990, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1import java.util.Iterator;
2import java.util.NoSuchElementException;
3
4public class LinkedList<E> implements Iterable<E> {
5
6 private static class Node<E> {
7 E data;
8 Node<E> next;
9
10 Node(E data, Node<E> next) {
11 this.data = data;
12 this.next = next;
13 }
14 }
15
16 private Node<E> head;
17 private int size;
18
19 public void addFirst(E element) {
20 head = new Node<>(element, head);
21 size++;
22 }
23
24 public E removeFirst() {
25 if (head == null) {
26 throw new NoSuchElementException("list is empty");
27 }
28 E data = head.data;
29 head = head.next;
30 size--;
31 return data;
32 }
33
34 public E getFirst() {
35 if (head == null) {
36 throw new NoSuchElementException("list is empty");
37 }
38 return head.data;
39 }
40
41 public int size() {
42 return size;
43 }
44
45 public boolean isEmpty() {
46 return size == 0;
47 }
48
49 @Override
50 public Iterator<E> iterator() {
51 return new Iterator<E>() {
52 private Node<E> current = head;
53
54 @Override
55 public boolean hasNext() {
56 return current != null;
57 }
58
59 @Override
60 public E next() {
61 if (current == null) {
62 throw new NoSuchElementException();
63 }
64 E data = current.data;
65 current = current.next;
66 return data;
67 }
68 };
69 }
70
71 public static void main(String[] args) {
72 LinkedList<Integer> list = new LinkedList<>();
73 for (int i = 0; i < 10; i++) {
74 list.addFirst(i);
75 }
76 for (int val : list) {
77 System.out.print(val + " ");
78 }
79 System.out.println();
80 }
81}