Advertisement
jtentor

DemoList5 - DoubleLinkedListIteratorComparable.java

Jun 8th, 2020
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  2. //
  3. import java.lang.Comparable;
  4. import java.util.Iterator;
  5.  
  6. public class DoubleLinkedListIteratorComparable<ELEMENT extends Comparable<ELEMENT>> implements Iterable<ELEMENT> {
  7.  
  8.     private class Node {
  9.         public ELEMENT item;
  10.         public Node next;
  11.         public Node prev;
  12.  
  13.         public Node() {
  14.             this(null, null, null);
  15.         }
  16.         public Node(ELEMENT item) {
  17.             this(item, null, null);
  18.         }
  19.         public Node(ELEMENT item, Node next) {
  20.             this(item, next, null);
  21.         }
  22.         public Node(ELEMENT item, Node next, Node prev) {
  23.             this.item = item;
  24.             this.next = next;
  25.             this.prev = prev;
  26.         }
  27.  
  28.         @Override
  29.         public String toString() {
  30.             return this.item.toString();
  31.         }
  32.     }
  33.  
  34.     private Node head;
  35.     private int count;
  36.     private Node tail;
  37.  
  38.     public int size() {
  39.         return this.count;
  40.     }
  41.  
  42.     public DoubleLinkedListIteratorComparable() {
  43.         this.head = null;
  44.         this.count = 0;
  45.         this.tail = null;
  46.     }
  47.  
  48.     public void addFirst(ELEMENT item) {
  49.         Node temp = new Node(item, this.head, null);
  50.         if (this.count == 0) {
  51.             this.tail = temp;
  52.         }
  53.         else {
  54.             this.head.prev = temp;
  55.         }
  56.         this.head = temp;
  57.         ++this.count;
  58.     }
  59.  
  60.     public void addLast(ELEMENT item) {
  61.         Node temp = new Node(item, null, this.tail);
  62.         if (this.count == 0) {
  63.             this.head = temp;
  64.         }
  65.         else {
  66.             this.tail.next = temp;
  67.         }
  68.         this.tail = temp;
  69.         ++this.count;
  70.     }
  71.  
  72.     public ELEMENT removeFirst() {
  73.         if (this.count == 0) {
  74.             throw new RuntimeException("La lista está vacía...");
  75.         }
  76.         ELEMENT item = this.head.item;
  77.         this.head = this.head.next;
  78.         if (this.head == null) {
  79.             this.tail = null;
  80.         }
  81.         else {
  82.             this.head.prev = null;
  83.         }
  84.         --this.count;
  85.         return item;
  86.     }
  87.  
  88.     public ELEMENT removeLast() {
  89.         if (this.count == 0) {
  90.             throw new RuntimeException("La lista está vacía...");
  91.         }
  92.         ELEMENT item = this.tail.item;
  93.         if (this.head.next == null) {
  94.             this.head = this.tail = null;
  95.         } else {
  96.             this.tail = this.tail.prev;
  97.             this.tail.next = null;
  98.         }
  99.         --this.count;
  100.         return item;
  101.     }
  102.  
  103.  
  104.     @Override
  105.     public Iterator<ELEMENT> iterator() {
  106.         return new MyIterator(this.head);
  107.     }
  108.  
  109.  
  110.     private class MyIterator implements Iterator<ELEMENT> {
  111.         private Node current;
  112.  
  113.         public MyIterator(Node current) {
  114.             this.current = current;
  115.         }
  116.  
  117.         @Override
  118.         public boolean hasNext() {
  119.             return this.current != null;
  120.         }
  121.  
  122.         @Override
  123.         public ELEMENT next() {
  124.             if (!this.hasNext()) {
  125.                 throw new RuntimeException("La lista está vacía...");
  126.             }
  127.             ELEMENT item = this.current.item;
  128.             this.current = this.current.next;
  129.             return item;
  130.         }
  131.     }
  132.  
  133.  
  134.     public void addInOrder(ELEMENT item) {
  135.         if (this.count == 0) {
  136.             this.head = this.tail = new Node(item, null, null);
  137.             ++this.count;
  138.         }
  139.         else {
  140.             if (item.compareTo(this.head.item) <= 0) {
  141.                 this.addFirst(item);
  142.             }
  143.             else {
  144.                 if (item.compareTo(this.tail.item) > 0) {
  145.                     this.addLast(item);
  146.                 }
  147.                 else {
  148.                     Node skip = this.head;
  149.                     while ((skip != null) && (item.compareTo(skip.item) > 0)) {
  150.                         skip = skip.next;
  151.                     }
  152.                     if (skip == null) {
  153.                         throw new RuntimeException("Algo está mal en el orden de los elementos de la lista...");
  154.                     }
  155.                     else {
  156.                         Node temp = new Node(item, skip, skip.prev);
  157.                         skip.prev.next = temp;
  158.                         skip.prev = temp;
  159.                         ++this.count;
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.  
  167.     public boolean findAndRemove(ELEMENT item) {
  168.         if (this.count == 0) {
  169.             return false;
  170.         }
  171.  
  172.         Node skip = this.head;
  173.         while ((skip != null) && !(item.compareTo(skip.item) == 0)) {
  174.             skip = skip.next;
  175.         }
  176.         if (skip == null) {
  177.             return false;
  178.         }
  179.         else {
  180.             if (skip.prev == null) {
  181.                 this.removeFirst();
  182.                 return true;
  183.             }
  184.             else {
  185.                 if (skip.next == null) {
  186.                     this.removeLast();
  187.                     return true;
  188.                 }
  189.                 else {
  190.                     skip.prev.next = skip.next;
  191.                     skip.next.prev = skip.prev;
  192.                     skip.prev = skip.next = null;
  193.                     return true;
  194.                 }
  195.             }
  196.         }
  197.     }
  198.  
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement