package fr.umlv.conc;

import java.util.function.Consumer;

public class StringList {
  static final class Entry {
    final String element;
    Entry next;
    
    Entry(String element) {
      this.element = element;
    }
  }
  
  private final Entry head;
  //private Entry tail;
  
  public StringList() {
    /*tail = */ head = new Entry(null); // fake first entry
  }
  
  public void forEach(Consumer<? super String> consumer) {
    for(Entry e = head.next; e != null; e = e.next) {
      consumer.accept(e.element);
    }
  }
  
  public void addLast(String element) {
    Entry entry = new Entry(element);
    Entry last = head;
    for(;;) {
      Entry next = last.next;
      if (next == null) {
        last.next = entry;
        return;
      }
      last = next;
    }
  }
  
  public static void main(String[] args) {
    StringList list = new StringList();
    list.addLast("foo");
    list.addLast("bar");
    list.addLast("baz");
    list.forEach(System.out::println);
  }
}
