Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.74 KB | None | 0 0
  1. package structures;
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class RecursiveList<T> implements ListInterface<T> {
  6.  
  7.   private class Node<T>{
  8.     T data;
  9.     Node<T> next;
  10.    
  11.     public Node(T data) {
  12.       this.data = data;
  13.       this.next = null;
  14.     }
  15.   }
  16.  
  17.   private class NodeIterator<T> implements Iterator<T>{
  18.     private Node<T> curr;
  19.     public NodeIterator() {
  20.       curr = front;
  21.     }
  22.    
  23.     public boolean hasNext() {
  24.       return curr.next != null;
  25.     }
  26.    
  27.     public T next() {
  28.       T temp = curr.data;
  29.       curr = curr.next;
  30.       return temp;
  31.     }
  32.    
  33.     @Override
  34.     public void remove() {
  35.       throw new UnsupportedOperationException();
  36.     }
  37.   }
  38.  
  39.   private int size;
  40.   public Node front;
  41.  
  42.   public <T> RecursiveList() {
  43.     this.size = 0;
  44.     front = new Node<T>(null);
  45.   }
  46.  
  47.   @Override
  48.   public int size() {
  49.     return this.size;
  50.   }
  51.  
  52.   private Node<T> findNode(int index, int count, Node curr) {
  53.     if (index == count || curr.next == null)
  54.       return curr;
  55.     return findNode(index, count + 1, curr.next);
  56.   }
  57.  
  58.   @Override
  59.   public ListInterface<T> insertFirst(T elem) {
  60.     if (elem == null)
  61.       throw new NullPointerException();
  62.     return insertAt(0, elem);
  63.   }
  64.  
  65.   @Override
  66.   public ListInterface<T> insertLast(T elem) {
  67.     if (elem == null)
  68.       throw new NullPointerException();
  69.    
  70.     if(size() == 0) {
  71.       return insertFirst(elem);
  72.     }
  73.     return insertAt(size(), elem);
  74.   }
  75.  
  76.   @Override
  77.   public ListInterface<T> insertAt(int index, T elem) throws IndexOutOfBoundsException {
  78.     if ((index < 0) || (index > size))
  79.       throw new IndexOutOfBoundsException();
  80.     if (elem == null)
  81.       throw new NullPointerException();
  82.     Node<T> node = new Node<T>(elem);
  83.     size++;
  84.    
  85.     if (size == 0) {
  86.       front = node;
  87.       return this;
  88.     }
  89.    
  90.     if (index == 0) {
  91.       if (size == 0) {
  92.         this.front = node;
  93.       } else {
  94.         node.next = front;
  95.         front= node;
  96.       }
  97.       return this;
  98.     }
  99.    
  100.     Node<T> prev = findNode(index - 1, 0, front);
  101.     if (index == size) {
  102.       prev.next = node;
  103.     } else {
  104.       Node<T> temp = prev.next;
  105.       prev.next = node;
  106.       node = temp;
  107.     }
  108.     return this;
  109.   }
  110.  
  111.   @Override
  112.   public T removeFirst() {
  113.     if(isEmpty()) throw new IllegalStateException();
  114.     return removeAt(0);
  115.   }
  116.  
  117.   @Override
  118.   public T removeLast() {
  119.     if(isEmpty()) throw new IllegalStateException();
  120.     if(size() == 1) {
  121.       return removeFirst();
  122.     }
  123.     return removeAt(size() - 1);
  124.   }
  125.  
  126.   @Override
  127.   @SuppressWarnings("unchecked")
  128.   public T removeAt(int index) {
  129.     if(isEmpty()) throw new IllegalStateException();
  130.     if (index < 0 || index >= size())
  131.       throw new IndexOutOfBoundsException();
  132.     this.size--;
  133.     if(index == 0) {
  134.       T ret = (T) front.data;
  135.       if(size == 1 ) {
  136.         front = null;
  137.       } else {
  138.         front = front.next;
  139.       }
  140.       return ret;
  141.     }
  142.    
  143.     Node<T> prev = findNode(index - 1, 0, front);
  144.     T ret = prev.next.data;
  145.     prev.next = prev.next.next;
  146.    
  147.     return ret;
  148.   }
  149.  
  150.   @Override
  151.   public T getFirst() {
  152.     if(isEmpty()) throw new IllegalStateException();
  153.     return get(0);
  154.   }
  155.  
  156.   @Override
  157.   public T getLast() {
  158.     if(isEmpty()) throw new IllegalStateException();
  159.     return get(size() - 1);
  160.   }
  161.  
  162.   @Override
  163.   public T get(int index) {
  164.     if(isEmpty())
  165.       throw new IllegalStateException();
  166.     if (index < 0 || index >= size())
  167.       throw new IndexOutOfBoundsException();
  168.    
  169.    
  170.    
  171.     return findNode(0, index, front).data;
  172.   }
  173.  
  174.  
  175.   public T peek() {
  176.     return (T) front.data;
  177.   }
  178.  
  179.   @Override
  180.   public boolean remove(T elem) {
  181.     if(elem == null)
  182.       throw new NullPointerException();
  183.     boolean del = removeHelper(front, 0, elem);
  184.     return del;
  185.   }
  186.  
  187.   private boolean removeHelper(Node<T> curr, int index, T elem) {
  188.     if(index > size() || curr == null) return false;
  189.     if(curr.data == elem) {
  190.       removeAt(index);
  191.       return true;
  192.     }
  193.    
  194.     return removeHelper(curr.next, index + 1, elem);
  195.   }
  196.  
  197.   @Override
  198.   public int indexOf(T elem) {
  199.     if(elem == null)
  200.       throw new NullPointerException();
  201.    
  202. //    if(elem == front.data) return 0;
  203.     return indexHelper(front, 0, elem);
  204.   }
  205.  
  206.   private int indexHelper(Node<T> curr, int index, T elem) {
  207.     if(curr.equals(null)) return -1;
  208.     if(curr.data.equals(elem)) {
  209.       return index;
  210.     }
  211.    
  212.     return indexHelper(curr.next, index + 1, elem);
  213.   }
  214.  
  215.  
  216.   @Override
  217.   public boolean isEmpty() {
  218.     return size() <= 0;
  219.   }
  220.  
  221.   @Override
  222.   public Iterator<T> iterator() {
  223.     // TODO Auto-generated method stub
  224.     return new NodeIterator<T>();
  225.   }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement