Advertisement
Jurado001

TP4ej3_Lista

Oct 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. import java.util.Iterator;
  2.  
  3. public class List<E> implements Iterable<E> {
  4.     private Node head;
  5.     private int count;
  6.     private Node tail;
  7.  
  8.    
  9.     public boolean isEmpty() {
  10.         return count <= 0;
  11.     }
  12.    
  13.     public int getCount() {
  14.         return this.count;
  15.     }
  16.    
  17.     //Metodos
  18.     public List() {
  19.         this.head = null;
  20.         this.count = 0;
  21.         this.tail = null;
  22.     }
  23.    
  24.    
  25.     public void AddToHead(E item) {
  26.         Node temp = new Node(item, this.head);
  27.         if(this.count == 0) {
  28.             this.tail = temp;
  29.         }
  30.         this.head = temp;
  31.         ++this.count;
  32.     }
  33.    
  34.     public void AddToTail(E item) {
  35.         Node temp = new Node(item, null);
  36.         if(this.count == 0) {
  37.             this.head = temp;
  38.         }
  39.         else {
  40.             this.tail.next = temp;
  41.         }
  42.         this.tail = temp;
  43.         ++this.count;
  44.     }
  45.    
  46.    
  47.    
  48.     public E RemoveFromHead() {
  49.         if(this.count == 0) {
  50.             throw new RuntimeException("La lista está vacia...");
  51.         }
  52.         E item = this.head.item;
  53.         this.head = this.head.next;
  54.         if(this.head == null) {
  55.             this.tail = null;
  56.         }
  57.         --this.count;
  58.         return item;
  59.     }
  60.    
  61.     public E RemoveFromTail() {
  62.         if(this.count == 0) {
  63.             throw new RuntimeException("La lista está vacia...");
  64.         }
  65.         E item = this.tail.item;
  66.         if(this.head.next == null) {
  67.             this.head = this.tail = null;
  68.         }
  69.         else {
  70.             Node skip = this.head;
  71.             for( ; skip.next.next != null; skip = skip.next) { }
  72.             this.tail = skip;
  73.             this.tail.next = null;
  74.         }
  75.         --this.count;
  76.         return item;
  77.     }
  78.    
  79.     public boolean Contains(Object item) {
  80.         Iterator<E>iter = iterator();
  81.         while(iter.hasNext()) {
  82.             if(iter.next().equals(item))
  83.                 return true;
  84.         }
  85.         return false;
  86.     }
  87.    
  88.     public void Mostrar() {
  89.         for(Node skip = this.head; skip != null; skip = skip.next) {
  90.             System.out.println(skip);
  91.         }
  92.     }
  93.    
  94.    
  95.     private class Node {
  96.         public E item;
  97.         public Node next;
  98.        
  99.         //Getters & Setters    
  100.         public E getItem() {
  101.             return item;
  102.         }
  103.  
  104.         public void setItem(E item) {
  105.             this.item = item;
  106.         }
  107.  
  108.         public Node getNext() {
  109.             return next;
  110.         }
  111.  
  112.         public void setNext(Node next) {
  113.             this.next = next;
  114.         }
  115.  
  116.         //Metodos
  117.         public Node() {
  118.             this(null, null);
  119.         }
  120.        
  121.         public Node(E item) {
  122.             this(item, null);
  123.         }
  124.        
  125.         public Node(E item, Node next) {
  126.             this.item = item;
  127.             this.next = next;
  128.         }
  129.        
  130.         public Node(Node next) {
  131.             this(null,next);
  132.         }
  133.        
  134.  
  135.         public String toString() {
  136.             return this.item.toString();
  137.         }
  138.        
  139.     }
  140.  
  141.     @Override
  142.     public Iterator<E> iterator() {
  143.         return new MyIterator(this.head);
  144.     }
  145.    
  146.     private class MyIterator implements Iterator<E>{
  147.         private Node actual;
  148.        
  149.         public MyIterator(Node inicio) {
  150.             this.actual = inicio;
  151.         }
  152.        
  153.         @Override
  154.         public boolean hasNext() {
  155.             return this.actual != null;
  156.         }
  157.        
  158.         @Override
  159.         public E next() {
  160.             if(!this.hasNext()) {
  161.                 throw new RuntimeException("La lista está vacia...");
  162.             }
  163.             E item = this.actual.item;
  164.             this.actual = this.actual.next;
  165.             return item;
  166.         }
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement