Guest User

Untitled

a guest
May 16th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3.  
  4. public class LinkedList<T> implements Iterable<T>{
  5.     private ListNode<T> front;
  6.     private int size = 0;
  7.    
  8.     public LinkedList(){
  9.         front = null;
  10.     }
  11.    
  12.     public void add(T obj){
  13.         ListNode<T> node = new ListNode<T>(obj, null);
  14.         ListNode<T> curr;
  15.        
  16.         if(front == null){
  17.             front = node;
  18.         }
  19.         else{
  20.             curr = front;
  21.            
  22.             while(curr.getNext() != null){
  23.                 curr = curr.getNext();
  24.             }
  25.            
  26.             curr.setNext(node);
  27.         }
  28.        
  29.         size++;
  30.     }
  31.    
  32.     public void add(int index, T obj){
  33.         if(index == 0){
  34.             addFirst(obj);
  35.         }
  36.         else{
  37.             ListNode<T> node = new ListNode<T>(obj, null);
  38.             ListNode<T> curr = front;
  39.             int c = 0;
  40.            
  41.             while(c + 1 != index && curr.getNext() != null){
  42.                 curr = curr.getNext();
  43.                 c++;
  44.             }
  45.            
  46.             ListNode<T> next = curr.getNext();
  47.             curr.setNext(node);
  48.             node.setNext(next);
  49.            
  50.             size++;
  51.         }
  52.     }
  53.    
  54.     public void addFirst(T obj){
  55.         front = new ListNode<T>(obj, front);
  56.     }
  57.    
  58.     public T get(int index){       
  59.         if(front == null){
  60.             return null;
  61.         }
  62.         else{
  63.             ListNode<T> cNode = front;
  64.             int curr = 0;
  65.            
  66.             while(cNode.getNext() != null && curr != index){
  67.                 cNode = cNode.getNext();
  68.                 curr++;
  69.             }
  70.            
  71.             return cNode.getVal();
  72.         }
  73.     }
  74.    
  75.     public int size(){
  76.         return size;
  77.     }
  78.    
  79.     public String toString(){
  80.         String result = "";
  81.         ListNode<T> curr = front;
  82.        
  83.         while(curr.getNext() != null){         
  84.             result += curr.toString() + "\n";
  85.             curr = curr.getNext();
  86.         }
  87.        
  88.         return result;
  89.     }
  90.    
  91.     private class Helper implements Iterator<T>{
  92.         private int index = 0;
  93.        
  94.         public boolean hasNext(){
  95.             return (index < size);
  96.         }
  97.  
  98.         public void remove(){
  99.             throw new UnsupportedOperationException();
  100.         }
  101.        
  102.         public T next(){
  103.             if(hasNext()){
  104.                 index++;
  105.                 return get(index);
  106.             }
  107.             else{
  108.                 throw new NoSuchElementException();
  109.             }
  110.         }
  111.     }
  112.  
  113.     public Iterator<T> iterator(){
  114.         return new Helper();
  115.     }
  116. }
Add Comment
Please, Sign In to add comment