Share Pastebin
Guest
Public paste!

MyLinkedList.java

By: a guest | Mar 17th, 2010 | Syntax: Java | Size: 3.97 KB | Hits: 205 | Expires: Never
Copy text to clipboard
  1. package Proj2;
  2.  
  3. import java.util.*;
  4.  
  5. public class MyLinkedList<AnyType> implements Iterable<AnyType>
  6. {
  7.         //1. nested Node class
  8.         private static class Node<AnyType>
  9.         {
  10.                 public Node(AnyType d, Node<AnyType> p, Node<AnyType> n)
  11.                 {data = d; prev = p; next = n;}
  12.                
  13.                 public AnyType                  data;
  14.                 public Node<AnyType>    prev;
  15.                 public Node<AnyType>    next;
  16.         }//end of Node class
  17.        
  18.         //2. data fields and accessors
  19.         private int theSize;
  20.         private int modCount = 0;
  21.         private Node<AnyType> beginMarker;
  22.         private Node<AnyType> endMarker;
  23.        
  24.         public int size()
  25.         {return theSize;}
  26.        
  27.         public boolean isEmpty()
  28.         {return size() == 0;}
  29.        
  30.         //3. constructors
  31.         public MyLinkedList()
  32.         {clear();}
  33.        
  34.         //method clear: resets collection to 0
  35.         public void clear()
  36.         {
  37.                 beginMarker = new Node<AnyType>(null,null,null);
  38.                 endMarker = new Node<AnyType>(null,beginMarker,null);
  39.                 beginMarker.next = endMarker;
  40.                
  41.                 theSize = 0;
  42.                 modCount++;
  43.         }//end clear
  44.        
  45.         //4. more accessors and mutators
  46.         public boolean add(AnyType x)
  47.         {add(size(), x); return true;}
  48.        
  49.         public void add(int idx, AnyType x)
  50.         {addBefore(getNode(idx), x);}
  51.        
  52.         public AnyType get(int idx)
  53.         {return getNode(idx).data;}
  54.        
  55.         public AnyType set(int idx, AnyType newVal)
  56.         {
  57.                 Node<AnyType> p = getNode(idx);
  58.                 AnyType oldVal = p.data;
  59.                 p.data = newVal;
  60.                 return oldVal;
  61.         }
  62.        
  63.         public AnyType remove(int idx)
  64.         {return remove(getNode(idx));}
  65.        
  66.         //5. getNode method
  67.         private Node<AnyType> getNode(int idx)
  68.         {
  69.                 Node<AnyType> p;
  70.                 if((idx<0)||(idx>size()))
  71.                 {throw new IndexOutOfBoundsException();}
  72.                 if(idx<(size()/2))
  73.                 {
  74.                         p = beginMarker.next;
  75.                         for(int i=0;i<idx;i++)
  76.                         {p = p.next;}
  77.                 }
  78.                 else
  79.                 {
  80.                         p = endMarker;
  81.                         for(int i=size();i>idx;i--)
  82.                         {p = p.prev;}
  83.                 }
  84.                 return p;
  85.         }//end getNode method
  86.        
  87.         //6. addBefore method
  88.         private void addBefore(Node<AnyType> p, AnyType x)
  89.         {
  90.                 Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
  91.                 newNode.prev.next = newNode;
  92.                 p.prev = newNode;
  93.                 theSize++;
  94.                 modCount++;    
  95.         }//end addBefore method
  96.        
  97.         //7. remove and iterator methods
  98.         private AnyType remove( Node<AnyType> p)
  99.         {
  100.                 p.next.prev = p.prev;
  101.                 p.prev.next = p.next;
  102.                 theSize--;
  103.                 modCount++;
  104.                
  105.                 return p.data;
  106.         }//end remove method
  107.        
  108.         //required by the Iterable interface
  109.         public java.util.Iterator<AnyType> iterator()
  110.         {return new LinkedListIterator<AnyType>();}
  111.        
  112.         //8. LinkedListIterator class
  113.         private class LinkedListIterator<AnyType> implements Iterator<AnyType>
  114.         {
  115.                 private Node<AnyType> current = (Node<AnyType>) beginMarker.next;
  116.                
  117.                 //used to check for modifications to list
  118.                 private int expectedModCount = modCount;
  119.                 private boolean okToRemove = false;
  120.                
  121.                 public boolean hasNext()
  122.                 {return current != endMarker;}
  123.                
  124.                 public AnyType next()
  125.                 {
  126.                         System.out.println("modcount: "+modCount+" expectedModCount: "+expectedModCount);
  127.                         if(modCount != expectedModCount)
  128.                         {throw new ConcurrentModificationException();}
  129.                        
  130.                         if(!hasNext())
  131.                         {throw new NoSuchElementException();}
  132.                        
  133.                         AnyType nextItem = current.data;
  134.                         current = current.next;
  135.                         okToRemove = true;
  136.                         return nextItem;
  137.                 }//end of next method
  138.                
  139.                 public void remove()
  140.                 {
  141.                         if(modCount != expectedModCount)
  142.                         {throw new ConcurrentModificationException();}
  143.                         if(!okToRemove)
  144.                         {throw new IllegalStateException();}
  145.                         MyLinkedList.this.remove(indexOf(current.prev));
  146.                         okToRemove = false;
  147.                         ++expectedModCount;
  148.                 }//end of remove method
  149.  
  150.                 private int indexOf(Node<AnyType> x)
  151.                 {
  152.                         /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  153.                          * @self: likeliest source of OBOES.  Be sure to look here first if fit hits shan.
  154.                          */
  155.                         int idx = 0;
  156.                         Node<AnyType> searchCurrent = (Node<AnyType>) beginMarker;
  157.                         while((searchCurrent!= endMarker/*hasNext*/)&&(!searchCurrent.equals(x)))
  158.                         {idx++;searchCurrent = searchCurrent.next;}
  159.                        
  160.                         return idx;
  161.                 }//end of indexOf method
  162.         }//end of LinkedListIterator class     
  163. }//end of MyLinkedList class