/**
 * Implemented as a linear linked list of nodes with front and back ends.
 *
 * @author Zach Tomaszewski
 * @param <E>  The data type of elements to be stored in this Queue.
 */
public class LinearQueue<E> implements Queue<E> {

  //new items are added to back and removed from front.
  //Node links should go from back towards front.
  private Node<E> front;
  private Node<E> back;

  /**
   * Constructs a new empty Queue.
   */
  public LinearQueue() {
    this.front = null;
    this.back = null;
  }

  @Override
  public void offer(E item) {
    if (this.back == null) {
      this.back = new Node<E>(item);
      this.front = this.back;
    }else {
      this.back.setNext(new Node<E>(item));
      this.back = this.back.getNext();
    }
  }

  @Override
  public E peek() {

//IMPLEMENT THIS

  }

  @Override
  public E poll() {

//IMPLEMENT THIS

  }

  @Override
  public int size() {
    //not very efficient, but means we don't need to track size elsewhere
    int size = 0;
    for (Node<E> curr = this.front; curr != null; curr = curr.getNext()) {
      size++;
    }
    return size;
  }

  /**
   * Returns a String showing the contents of the queue from front to back.
   */
  @Override
  public String toString() {
    //StringBuilder = an optional but more efficient way to build a String
    StringBuilder str = new StringBuilder();
    str.append("[");
    for (Node<E> curr = this.front; curr != null; curr = curr.getNext()) {
      str.append(curr.getData());
      if (curr.getNext() != null) {
        str.append(", ");
      }
    }
    str.append("]");
    return str.toString();
  }
}