Guest User

IteratorList Class

a guest
Mar 8th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. package jasonandrewsca3;
  2.  
  3. import java.util.ListIterator;
  4.  
  5. public class IteratorList<E> {
  6.     private IteratorList list;  
  7.     //private Node next = (Node) list.next();
  8.     private int length = 0;
  9.     private Node head;
  10.     private Node tail;
  11.    
  12.     Node<E> current = head;
  13.    
  14.     public ListIterator i;
  15.  
  16.    
  17.    
  18.     // your own class is going to give this out
  19.     public IteratorList()
  20.     {
  21.         current = null;
  22.     }
  23.    
  24.     public IteratorList(Node current)
  25.     {
  26.         this.current = current;
  27.     }
  28.    
  29.     public boolean isEmpty() {
  30.         if (length == 0){        
  31.             //System.out.print("Yes");
  32.             return true;  
  33.         }        else {
  34.             //System.out.print("No");
  35.             return false;
  36.         }
  37.     }
  38.        
  39.     /*public int size() {
  40.         return length;      
  41.     }*/
  42.     public Node<E> size(int index) throws IndexOutOfBoundsException {
  43.     if (index < 0 || index > length) {
  44.         throw new IndexOutOfBoundsException();
  45.     } else {
  46.         Node<E> cursor = head;
  47.         for (int i = 0; i < index; i++) {
  48.             cursor = cursor.getNext();
  49.         }
  50.         return cursor;
  51.     }
  52.     }    
  53.    
  54.     public boolean hasNext() {
  55.         if(head.next != null) {
  56.             //System.out.print("Is there any next object? Answer = ");
  57.             return true;
  58.         }
  59.         else {
  60.                 //System.out.print("Is there any next object? Answer = ");
  61.                 return false;
  62.             }
  63.     }
  64.    
  65.  
  66.  
  67.     public boolean hasPrevious() {
  68.         if(current.prev != null){
  69.             //System.out.print("Is there any previous object? Answer = ");
  70.             return true;
  71.         }
  72.         else {
  73.                 //System.out.print("Is there any previous object? Answer = ");
  74.                 return false;
  75.              }
  76.     }
  77.  
  78.     public Object next()
  79.     {
  80.         if(!hasNext()) {
  81.             return null;
  82.         }
  83.         current = current.next;
  84.         return current.data;
  85.     }
  86.  
  87.     public Object previous()  
  88.     {
  89.         if(!hasPrevious()) {
  90.             return null;
  91.         }
  92.         current = current.prev;
  93.         return current.data;
  94.     }
  95.     Node newNode = new Node();
  96.     public int nextIndex() throws IndexOutOfBoundsException{        
  97.         if(!current.next(newNode))
  98.         {
  99.             return (int)current.next();
  100.         }
  101.         else{
  102.                 return length;
  103.             }
  104.     }
  105.  
  106.     public int previousIndex() {
  107.         throw new IndexOutOfBoundsException("Not supported yet.");
  108.     }
  109.  
  110.     /*public void remove() {
  111.         length--;
  112.     }*/
  113.    
  114.     public void remove(Node node) throws IndexOutOfBoundsException{
  115.     // Handle the case when removing the head node from the list.
  116.     if( node.prev == null ) {
  117.       head = node.next; // Same as head = head.next
  118.     } else {
  119.       node.prev.next = node.next;
  120.     }
  121.     // Handle the case when removing the tail node from the list.
  122.     if( node.next == null ) {
  123.       tail = node.prev; // Same as tail = tail.prev
  124.     } else {
  125.       node.next.prev = node.prev;
  126.     }
  127.     length--;
  128.   }
  129.    
  130.  
  131.     public void set(int value, E e) throws IndexOutOfBoundsException {
  132.        
  133.     }
  134.  
  135.  
  136.     public void add(int index, E value) throws IndexOutOfBoundsException {
  137.     Node<E> cursor = size(index);
  138.     Node<E> temp = new Node<>(value);
  139.     temp.setPrev(cursor);
  140.     temp.setNext(cursor.getNext());
  141.     cursor.getNext().setPrev(temp);
  142.     cursor.setNext(temp);
  143.     length++;
  144.     }
  145.    
  146.     public void insertBefore(Node node, Node insert)
  147. {
  148.     Node prevNode = node.prev;
  149.  
  150.     insert.next = node;
  151.     insert.prev = prevNode;
  152.  
  153.     if(prevNode != null){
  154.         prevNode.next = insert;
  155.     }
  156.     node.prev = insert;
  157. }
  158.  
  159.     @Override
  160.     public String toString() {
  161.         StringBuffer result = new StringBuffer();
  162.                 Node<E> temp = head;
  163.                 int index = list.length;
  164.         while (temp.getNext() != list.size(index)) {
  165.                 temp = temp.getNext();
  166.                 result.append(temp.getValue()).append(" - ");
  167.         }
  168.         return result.toString();
  169.     }
  170.    
  171.    
  172.    
  173. }
Advertisement
Add Comment
Please, Sign In to add comment