/** ~mohammad/public_html/classes/csc241/List/intList.java
   An integer list.  pos is used in most of the methods here and represents
   the position of a node that we are interested in.  pos needs to be 
   between 0 and N-1, where N is the number of elements in the list.
   All methods that need to validate pos return -1, if it is not in
   the range stated. They return 0 when pos is appropriate. 
   **/
package csc241.samples.List;
import java.io.*;
import java.lang.*;
import java.awt.*;
public class intList {

  private Node head=null; // head of the list and starts as empty
  /** Constructor **/
  public intList(){}
  /** Check for empty **/
  public boolean empty(){
    return head == null;
  }

  /** This method finds val in list and returns the position of val.
      return -1 if val is not found.  Also, returns the position of
      of the first occurance of val.
      **/
  public int find(int val) { 
    Node temp;
    int pos;
    for (temp=head,pos=0;temp != null;temp=temp.next,pos++)
        if (temp.element == val)
           return pos;
    return -1;
  }
  /** add an element to the end of the list **/
  public void append(int val) {
    if (head==null)  //the list is empty
       head = new Node(null,val);
    else {
      Node temp;
      for (temp=head;temp.next != null;temp=temp.next);//Find the last node
      temp.next = new  Node(null,val); //Add a new node at the end
    }
  }
  /** Create a new node place val in it.  Put the new node before the node 
      identified by its poition, pos and return 0.  Return -1 if pos is 
      invalid (i.e. there aren't enough nodes in the list).
      **/
  public int addBefore(int pos, int val) {
    if (head ==null) return -1;
    if (pos == 0) {
      head = new Node(head,val);
      return 0;
    }
    // setup so that temp always points to the node before the one that
    // we want to insert before.
    Node temp;
    int last;
    for (temp=head,last=0;temp.next != null;temp=temp.next,last++)
      if (last == (pos-1)) {//found the spot to insert
        temp.next = new Node (temp.next,val);
        return 0;
      }
    return -1;
  }
  /** just like the previous method, except the traversal goes one
      node farther.
      **/
  public int addAfter(int pos, int val) {
    if (head==null) return -1;
    Node temp;
    int last;
    for (temp=head,last=0;temp != null;temp=temp.next,last++)
      if (last == pos) {
        temp.next = new Node (temp.next,val);
        return 0;
      }
    return -1;
  }
  /** Remove node at position. **/
  public int remove(int pos) {
    if (head==null) return -1;
    if (pos==0) {
      head=head.next;
      return 0;
    }
    Node temp;
    int last;
    for (temp=head,last=0;temp.next != null;temp=temp.next,last++)
      if (last == (pos-1)) {//need to stop at node before the one we remove
        temp.next = temp.next.next;
        return 0;
      }
    return -1;
  }

  /** return the value at pos **/
  public int valAt (int pos) {
    if (head == null) return -1;
    Node temp;
    int last;
    for (temp=head,last=0;temp != null;temp=temp.next,last++)
        if (last == pos)
           return temp.element;
    return -1;
  }

  /** Return the position of the last element **/
  public int last() {
    if (head == null) return -1;
    Node temp;
    int l;
    for (temp=head,l=0;temp.next != null;temp=temp.next,l++);
    return l;
  }

  /** return a string with the list content for debugging **/
  public String dump () {
    String text;
    if (head == null) 
      text = new String ("List is Empty");
    else {
      text= new String("head-> ");
      Node temp;
      int last;
      for (temp=head,last=0;temp != null;temp=temp.next,last++)
          text=text.concat(new String(temp.element + "-> "));
    }
    return text;
  }
}
