All pastes #285852 Raw Edit

/20/lab/SnglyLnkdLst

public java v1 · immutable
#285852 ·published 2006-12-20 02:48 UTC
rendered paste body
// Implements a singly-linked list.// Jim Herrero (I'm tired.)import java.util.Iterator;public class SinglyLinkedList implements Iterable<Object>{  private ListNode head;  private int nodeCount;  // Constructor: creates an empty list  public SinglyLinkedList()  {    head = null;    nodeCount = 0;  }  // Constructor: creates a list that contains  // all elements from the array values, in the same order  public SinglyLinkedList(Object[] values)  {    ListNode tail = null;    for (Object value : values) // for each value to insert    {      ListNode node = new ListNode(value, null);      if (head == null)        head = node;      else        tail.setNext(node);      tail = node;    // update tail    }    nodeCount = values.length;  }  // Returns true if this list is empty; otherwise returns false.  public boolean isEmpty()  {    return head == null;  }  // Returns the number of elements in this list.  public int size()  {    return nodeCount;  }  // Returns true if this list contains an element equal to obj;  // otherwise returns false.  public boolean contains(Object obj)  {    if(isEmpty())      return false;    boolean isFound = false;    for(Object o: this)      if(o.equals(obj));        isFound = true;    return isFound;  }  // Returns the index of the first element in equal to obj;  // if not found, retunrs -1.  public int indexOf(Object obj)  {    if(isEmpty())      return -1;    int position = 0;    for(Object o: this)      if(o.equals(obj))        return position;      else        position++;    return -1;  }  // Adds obj to this collection.  Returns true if successful;  // otherwise returns false.  public boolean add(Object obj)  {    if(isEmpty())    {      ListNode node = new ListNode(obj, null);      head = node;      nodeCount = 1;      return true;    }    ListNode node = new ListNode(obj, null);    ListNode a = head;    while(a.getNext() != null)      a = a.getNext();    a.setNext(node);    node.setNext(null);    nodeCount++;    return true;  }  // Removes the first element that is equal to obj, if any.  // Returns true if successful; otherwise returns false.  public boolean remove(Object obj)  {    if(isEmpty())      return false;    ListNode a = head;    ListNode b = head.getNext();    while(b != null)    {      if(b.getValue().equals(obj))      {        a.setNext(b.getNext());        nodeCount--;        return true;      }      a = b;      b = a.getNext();    }    return false;  }  // Returns the i-th element.                 public Object get(int i)  {    if(isEmpty() || i > nodeCount - 1)      return null;    int position = 0;    for(Object o : this)      if(position == i)        return o;      else        position++;    return -1;  }  // Replaces the i-th element with obj and returns the old value.  public Object set(int i, Object obj)  {    if(isEmpty() || i > nodeCount - 1)      return null;    ListNode node = new ListNode(obj, null);    ListNode a = head;    ListNode b = head.getNext();    int count = 0;    while(b != null)    {      if(b.getValue().equals(obj))      {        a.setNext(node);        node.setNext(b.getNext());        return b.getValue();      }      a = b;      b = a.getNext();      count++;    }    return null;  }  // Inserts obj to become the i-th element. Increments the size  // of the list by one.  public void add(int i, Object obj)  {    if(isEmpty() && (i == 0))    {      add(obj);      return;    }    if(isEmpty() || i > nodeCount)      return;    ListNode node = new ListNode(obj, null);    ListNode a = head;    ListNode b = head.getNext();    int count = 0;    while(b != null)    {      if(count == i)      {        a.setNext(node);        node.setNext(b);        nodeCount++;        return;      }      a = b;      b = a.getNext();      count++;    }  }  // Removes the i-th element and returns its value.  // Decrements the size of the list by one.  public Object remove(int i)  {    if(isEmpty() || i > nodeCount - 1)      return null;    ListNode a = head;    ListNode b = head.getNext();    int count = 0;    while(b != null)    {      if(count == i)      {        a.setNext(b.getNext());        nodeCount--;        return b.getValue();      }      a = b;      b = a.getNext();      count++;    }    return null;  }  // Returns a string representation of this list.  public String toString()  {    String returnvalue = "[ ";    for(Object o : this)      returnvalue += o + " ";    returnvalue += "]";    return returnvalue;  }  // Returns an iterator for this collection.  public Iterator<Object> iterator()  {    return new SinglyLinkedListIterator(head);  }}