/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package generics; import java.util.Iterator; /** * * @author steve */ public class GenericLinkedList implements MinGenericList { GenericNode head, tail; @Override public void add(V v) { if(head == null) { head = tail = new GenericNode<>(v); } else { tail.next = new GenericNode<>(v); tail = tail.next; } } @Override public int size() { int n = 0; for(var x = head; x != null; x = x.next) { n++; } return n; } @Override public V get(int n) { for(var x = head; x != null;x = x.next) { if(n-- == 0) { return x.v; } } return null; } @Override public void set(int n, V v) { for(var x = head; x != null;x = x.next) { if(n-- == 0) { x.v = v; } } } @Override public Iterator iterator() { return new Iterator<>() { GenericNode x = new GenericNode<>(null,head); @Override public boolean hasNext() { return x.next != null; } @Override public V next() { x = x.next; return x.v; } }; } public static void main(String[] args) { MinGenericList list = new GenericLinkedList<>(); GenericListTest.test(list); } }