package com.tutego.insel.list;

public class LinkedList
{
  private Cell head, tail;

  static class Cell
  {
    Object data;
    Cell   next;
  }

  public void add( Object o )
  {
    Cell newCell = new Cell();
    newCell.data = o;

    if ( head == null )                // or tail == null
      head = tail = newCell;
    else
      tail = tail.next = newCell;
  }

  public void addAll( Object... os )
  {
    for ( Object o : os )
      add( o );
  }

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder( 1024 ).append( '[' );

    for ( Cell cell = head; cell != null; )
    {
      sb.append( cell.data );

      if ( cell.next != null )
        sb.append( ", " );

      cell = cell.next;
    }

    return sb.append(']').toString();
  }
}
