Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Clase Principal------------------------------------------------------------
- package tp4ej8;
- import java.util.Iterator;
- import java.util.Random;
- public class Principal {
- public static void main(String[] args) {
- List<Integer> listaPar = new List<Integer>();
- List<Integer> listaImpar = new List<Integer>();
- List<Integer> listaOriginal = new List<Integer>();
- CargarLista(listaOriginal);
- Separar(listaOriginal,listaPar,listaImpar);
- System.out.printf("\nLista Original: ");
- Mostrar(listaOriginal);
- System.out.printf("\n\nLista Par :");
- Mostrar(listaPar);
- System.out.printf("\n\nLista Impar : ");
- Mostrar(listaImpar);
- }
- public static void CargarLista(List<Integer> listaOriginal) {
- Random random = new Random();
- int num;
- for (int i = 0; i < 6; ++i) {
- num = random.nextInt(100);
- listaOriginal.AddInOrder(num);
- }
- }
- public static void Separar(List<Integer> listaOriginal,List<Integer> listaPar,List<Integer> listaImpar) {
- for (int n : listaOriginal) {
- if (n%2==0){
- listaPar.AddInOrder(n);
- }else {
- listaImpar.AddInOrder(n);
- }
- }
- }
- public static void Mostrar(List<Integer> lista){
- Iterator<Integer> iter = lista.iterator();
- while(iter.hasNext()) {
- System.out.printf("%d ", iter.next());
- }
- }
- }
- ---------------------------------------------------------------------------
- Clase Lista Ordenada--------------------------------------------------------
- package tp4ej8;
- import java.lang.Comparable;
- import java.util.Iterator;
- public class List<E extends Comparable<E>> implements Iterable<E> {
- private Node head;
- private int count;
- private Node tail;
- // @return cantidad de elementos en la lista
- public int getCount() {
- return this.count;
- }
- public List() {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- // Agrega al principio
- public void AddFirst(E item) {
- if (this.count == 0) {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else {
- Node temp = new Node(item, null, null);
- temp.next = this.head;
- this.head.prev = temp;
- this.head = temp;
- ++this.count;
- }
- }
- // Agrega al final
- public void AddLast(E item) {
- if (this.count == 0) {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else {
- Node temp = new Node(item, null, null);
- temp.prev = this.tail;
- this.tail.next = temp;
- this.tail = temp;
- ++this.count;
- }
- }
- //Agrega de forma ordenada
- public void AddInOrder(E item) {
- if (this.count == 0) {
- this.head = this.tail = new Node(item, null, null);
- ++this.count;
- }
- else {
- if (item.compareTo(this.head.item) <= 0) {
- // es menor o igual que el primero
- this.AddFirst(item);
- }
- else {
- if (item.compareTo(this.tail.item) > 0) {
- // es mayor que el último
- this.AddLast(item);
- }
- else {
- Node skip = this.head;
- for ( ; (skip != null) && (item.compareTo(skip.item) > 0); skip = skip.next) { }
- // skip es null o el nodo cuyo valor es mas grande que item
- if (skip == null) {
- // esto no debería ocurrir por las dudas hacemos programación defensiva
- throw new RuntimeException("Algo está mal en el orden de los elementos de la lista...");
- }
- else {
- // se debe insertar antes de skip
- Node temp = new Node(item, skip, skip.prev);
- skip.prev.next = temp;
- skip.prev = temp;
- ++this.count;
- }
- }
- }
- }
- }
- // Extrae y devuelve el primer elemento de la lista
- public E RemoveFirst() {
- if (this.count == 0) {
- throw new RuntimeException("La lista está vacía...");
- }
- E item = this.head.item;
- this.head = this.head.next;
- if (this.head == null) {
- this.tail = null;
- }
- else {
- this.head.prev = null;
- }
- --this.count;
- return item;
- }
- // Extrae y devuelve el último elemento de la lista
- public E RemoveLast() {
- if (this.count == 0) {
- throw new RuntimeException("La lista está vacía...");
- }
- E item = this.tail.item;
- if (this.head.next == null) {
- this.head = this.tail = null;
- } else {
- this.tail = this.tail.prev;
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public boolean FindAndRemove(E item) {
- if (this.count == 0) {
- return false;
- }
- Node skip = this.head;
- for ( ; (skip != null) && !(item.compareTo(skip.item) == 0); skip = skip.next) { }
- // skip es null o el nodo cuyo valor es igual que item
- if (skip == null) {
- // no esta
- return false;
- }
- else {
- // se debe extraer el nodo skip
- if (skip.prev == null) {
- // es el primero
- this.RemoveFirst();
- return true;
- }
- else {
- if (skip.next == null) {
- // es el último
- this.RemoveLast();
- return true;
- }
- else {
- skip.prev.next = skip.next;
- skip.next.prev = skip.prev;
- skip.prev = skip.next = null;
- return true;
- }
- }
- }
- }
- // Clase privada para los nodos de la lista
- private class Node {
- public E item;
- public Node next;
- public Node prev;
- public Node() {
- this(null, null, null);
- }
- public Node(E item) {
- this(item, null, null);
- }
- public Node(E item, Node next) {
- this(item, next, null);
- }
- public Node(E item, Node next, Node prev) {
- this.item = item;
- this.next = next;
- this.prev = prev;
- }
- public String toString() {
- return this.item.toString();
- }
- }
- public Iterator<E> iterator() {
- return new MyIterator(this.head);
- }
- // Implementación de un iterador para la clase List
- private final class MyIterator implements Iterator<E> {
- private Node current;
- public MyIterator(Node start) {
- this.current = start;
- }
- public boolean hasNext() {
- return this.current != null;
- }
- public E next() {
- if (!this.hasNext()) {
- throw new RuntimeException("La lista está vacía...");
- }
- E item = this.current.item;
- this.current = this.current.next;
- return item;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement