Advertisement
Guest User

Untitled

a guest
May 16th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1.     import java.util.Iterator;
  2.     public class LinkedList<T> implements Iterable<T>{
  3.        
  4.        
  5.         //Construct List/////////////////////////////////////////////////
  6.         private Node Head;
  7.         public LinkedList(){
  8.             Head = new Node(null,null,null);
  9.             Head.next = Head;
  10.             Head.prev = Head;
  11.         }
  12.         /////////////////////////////////////////////////////////////////
  13.    
  14.        
  15.        
  16.         //Append Method//////////////////////////////////////////////////
  17.         public void append(T _add){
  18.             Node cur = Head.prev;
  19.             cur.next = new Node(Head,cur,_add);
  20.             Head.prev = cur.next;
  21.         }
  22.         /////////////////////////////////////////////////////////////////
  23.        
  24.        
  25.        
  26.         //lenght/////////////////////////////////////////////////////////
  27.         public int length(){
  28.             int count = 0;
  29.             Node cur = Head;
  30.             while (cur.next != Head){
  31.                 cur = cur.next;
  32.                 count++;
  33.             }
  34.             return count;
  35.         }
  36.         /////////////////////////////////////////////////////////////////
  37.        
  38.         public int find(T _find){
  39.             Node cur = Head.next;
  40.             int i = 0;
  41.             while (cur.data != null){
  42.                 if (cur.data.equals(_find)){
  43.                     return i;
  44.                 }
  45.                 i++;
  46.                 cur = cur.next;
  47.             }
  48.             return -1;
  49.         }
  50.        
  51.         //index//////////////////////////////////////////////////////////
  52.         public T index(int _index){
  53.             Node cur = Head.next;
  54.             int index = _index;
  55.             while (index > 0){
  56.                 cur = cur.next;
  57.                 index--;
  58.             }
  59.             return cur.data;                       
  60.         }
  61.         /////////////////////////////////////////////////////////////////
  62.        
  63.        
  64.        
  65.         //toString///////////////////////////////////////////////////////
  66.         public String toString(){
  67.             return ":LinkedList:<["+format(Head.next)+"]>";
  68.         }
  69.         private String format(Node _cur){  
  70.             if (_cur.data == null){
  71.                 return "";
  72.             }
  73.             else{
  74.                 return _cur.data.toString()+","+format(_cur.next);
  75.             }
  76.         }
  77.         /////////////////////////////////////////////////////////////////
  78.        
  79.         public Iterator<T> iterator(){
  80.             return new Itr();
  81.         }
  82.            
  83.         private class Itr implements Iterator<T> {
  84.             Node cur = Head;
  85.             public boolean hasNext(){
  86.                 return cur.next != Head;
  87.             }
  88.             public T next(){
  89.                 cur = cur.next;
  90.                 return cur.data;
  91.             }
  92.         }
  93.            
  94.         //Node Class/////////////////////////////////////////////////////  
  95.         class Node{
  96.             Node next;
  97.             Node prev;
  98.             T data;
  99.             Node(Node _next, Node _prev, T _data){
  100.                 next = _next;
  101.                 prev = _prev;
  102.                 data = _data;
  103.             }
  104.         }
  105.         /////////////////////////////////////////////////////////////////
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement