Advertisement
nati_estrada

tp4ej8

Oct 22nd, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.82 KB | None | 0 0
  1. Clase Principal------------------------------------------------------------
  2. package tp4ej8;
  3.  
  4. import java.util.Iterator;
  5. import java.util.Random;
  6.  
  7. public class Principal {
  8.  
  9.     public static void main(String[] args) {
  10.         List<Integer> listaPar = new List<Integer>();
  11.         List<Integer> listaImpar = new List<Integer>();
  12.         List<Integer> listaOriginal = new List<Integer>();
  13.        
  14.  
  15.         CargarLista(listaOriginal);
  16.         Separar(listaOriginal,listaPar,listaImpar);
  17.         System.out.printf("\nLista Original: ");
  18.         Mostrar(listaOriginal);
  19.         System.out.printf("\n\nLista Par :");
  20.         Mostrar(listaPar);        
  21.         System.out.printf("\n\nLista Impar : ");
  22.         Mostrar(listaImpar);
  23.  
  24.     }
  25.    
  26.     public static void CargarLista(List<Integer> listaOriginal) {
  27.         Random random = new Random();
  28.         int num;
  29.         for (int i = 0; i < 6; ++i) {
  30.             num = random.nextInt(100);
  31.             listaOriginal.AddInOrder(num);
  32.         }
  33.     }
  34.    
  35.     public static void Separar(List<Integer> listaOriginal,List<Integer> listaPar,List<Integer> listaImpar) {
  36.         for (int n : listaOriginal) {
  37.            
  38.             if (n%2==0){
  39.                 listaPar.AddInOrder(n);
  40.             }else {
  41.                 listaImpar.AddInOrder(n);
  42.             }
  43.            
  44.         }
  45.     }
  46.    
  47.     public static void Mostrar(List<Integer> lista){
  48.         Iterator<Integer> iter = lista.iterator();
  49.         while(iter.hasNext()) {
  50.             System.out.printf("%d ", iter.next());
  51.         }
  52.     }
  53. }
  54. ---------------------------------------------------------------------------
  55.  
  56. Clase Lista Ordenada--------------------------------------------------------
  57. package tp4ej8;
  58.  
  59. import java.lang.Comparable;
  60. import java.util.Iterator;
  61.  
  62. public class List<E extends Comparable<E>> implements Iterable<E> {
  63.  
  64.     private Node head;
  65.     private int count;
  66.     private Node tail;
  67.  
  68.  
  69.      // @return cantidad de elementos en la lista
  70.     public int getCount() {
  71.         return this.count;
  72.     }
  73.  
  74.  
  75.     public List() {
  76.         this.head = null;
  77.         this.count = 0;
  78.         this.tail = null;
  79.     }
  80.  
  81.    
  82.  
  83.      // Agrega al principio
  84.     public void AddFirst(E item) {
  85.         if (this.count == 0) {
  86.             this.head = this.tail = new Node(item, null, null);
  87.             ++this.count;
  88.         }
  89.         else {
  90.             Node temp = new Node(item, null, null);
  91.             temp.next = this.head;
  92.             this.head.prev = temp;
  93.             this.head = temp;
  94.             ++this.count;
  95.         }
  96.     }
  97.  
  98.  
  99.  
  100.  
  101.      // Agrega al final
  102.     public void AddLast(E item) {
  103.         if (this.count == 0) {
  104.             this.head = this.tail = new Node(item, null, null);
  105.             ++this.count;
  106.         }
  107.         else {
  108.             Node temp = new Node(item, null, null);
  109.             temp.prev = this.tail;
  110.             this.tail.next = temp;
  111.             this.tail = temp;
  112.             ++this.count;
  113.         }
  114.     }
  115.    
  116.    
  117.     //Agrega de forma ordenada
  118.     public void AddInOrder(E item) {
  119.         if (this.count == 0) {
  120.             this.head = this.tail = new Node(item, null, null);
  121.             ++this.count;
  122.         }
  123.         else {
  124.             if (item.compareTo(this.head.item) <= 0) {
  125.                 // es menor o igual que el primero
  126.                 this.AddFirst(item);
  127.             }
  128.             else {
  129.                 if (item.compareTo(this.tail.item) > 0) {
  130.                     // es mayor que el último
  131.                     this.AddLast(item);
  132.                 }
  133.                 else {
  134.                     Node skip = this.head;
  135.                     for ( ; (skip != null) && (item.compareTo(skip.item) > 0); skip = skip.next) { }
  136.                     // skip es null o el nodo cuyo valor es mas grande que item
  137.                     if (skip == null) {
  138.                         // esto no debería ocurrir por las dudas hacemos programación defensiva
  139.                         throw new RuntimeException("Algo está mal en el orden de los elementos de la lista...");
  140.                     }
  141.                     else {
  142.                         // se debe insertar antes de skip
  143.                         Node temp = new Node(item, skip, skip.prev);
  144.                         skip.prev.next = temp;
  145.                         skip.prev = temp;
  146.                         ++this.count;
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.        
  152.     }
  153.  
  154.  
  155.      // Extrae y devuelve el primer elemento de la lista
  156.     public E RemoveFirst() {
  157.         if (this.count == 0) {
  158.             throw new RuntimeException("La lista está vacía...");
  159.         }
  160.  
  161.         E item = this.head.item;
  162.         this.head = this.head.next;
  163.         if (this.head == null) {
  164.             this.tail = null;
  165.         }
  166.         else {
  167.             this.head.prev = null;
  168.         }
  169.         --this.count;
  170.         return item;
  171.     }
  172.    
  173.    
  174.    
  175.  
  176.      // Extrae y devuelve el último elemento de la lista
  177.  
  178.     public E RemoveLast() {
  179.         if (this.count == 0) {
  180.             throw new RuntimeException("La lista está vacía...");
  181.         }
  182.  
  183.         E item = this.tail.item;
  184.  
  185.         if (this.head.next == null) {
  186.             this.head = this.tail = null;
  187.         } else {
  188.             this.tail = this.tail.prev;
  189.             this.tail.next = null;
  190.         }
  191.         --this.count;
  192.         return item;
  193.        
  194.     }
  195.    
  196.    
  197.     public boolean FindAndRemove(E item) {
  198.         if (this.count == 0) {
  199.             return false;
  200.         }
  201.  
  202.         Node skip = this.head;
  203.         for ( ; (skip != null) && !(item.compareTo(skip.item) == 0); skip = skip.next) { }
  204.         // skip es null o el nodo cuyo valor es igual que item
  205.         if (skip == null) {
  206.             // no esta
  207.             return false;
  208.         }
  209.         else {
  210.             // se debe extraer el nodo skip
  211.             if (skip.prev == null) {
  212.                 // es el primero
  213.                 this.RemoveFirst();
  214.                 return true;
  215.             }
  216.             else {
  217.                 if (skip.next == null) {
  218.                     // es el último
  219.                     this.RemoveLast();
  220.                     return true;
  221.                 }
  222.                 else {
  223.                     skip.prev.next = skip.next;
  224.                     skip.next.prev = skip.prev;
  225.                     skip.prev = skip.next = null;
  226.                     return true;
  227.                 }
  228.             }
  229.         }
  230.     }
  231.  
  232.  
  233.    
  234.    
  235.    
  236.    
  237.    
  238.  
  239.      // Clase privada para los nodos de la lista
  240.  
  241.     private class Node {
  242.  
  243.         public E item;
  244.         public Node next;
  245.         public Node prev;
  246.        
  247.  
  248.         public Node() {
  249.             this(null, null, null);
  250.         }
  251.  
  252.  
  253.         public Node(E item) {
  254.             this(item, null, null);
  255.         }
  256.  
  257.  
  258.         public Node(E item, Node next) {
  259.             this(item, next, null);
  260.         }
  261.        
  262.  
  263.         public Node(E item, Node next, Node prev) {
  264.             this.item = item;
  265.             this.next = next;
  266.             this.prev = prev;
  267.         }
  268.  
  269.  
  270.         public String toString() {
  271.             return this.item.toString();
  272.         }
  273.     }
  274.  
  275.  
  276.  
  277.     public Iterator<E> iterator() {
  278.         return new MyIterator(this.head);
  279.     }
  280.  
  281.  
  282.      // Implementación de un iterador para la clase List
  283.  
  284.     private final class MyIterator implements Iterator<E> {
  285.         private Node current;
  286.        
  287.         public MyIterator(Node start) {
  288.             this.current = start;
  289.         }
  290.        
  291.         public boolean hasNext() {
  292.             return this.current != null;
  293.         }
  294.  
  295.         public E next() {
  296.             if (!this.hasNext()) {
  297.                 throw new RuntimeException("La lista está vacía...");
  298.             }
  299.             E item = this.current.item;
  300.             this.current = this.current.next;
  301.             return item;
  302.         }
  303.        
  304.     }
  305.    
  306.    
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement