Advertisement
jtentor

DemoList4 - SimpleLinkedListIterator.java

Jun 8th, 2020
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  2. //
  3. import java.util.Iterator;
  4.  
  5. public class SimpleLinkedListIterator<ELEMENT> implements Iterable<ELEMENT> {
  6.  
  7.     private class Node {
  8.         public ELEMENT item;
  9.         public Node next;
  10.  
  11.         public Node() {
  12.             this(null, null);
  13.         }
  14.         public Node(ELEMENT item) {
  15.             this(item, null);
  16.         }
  17.         public Node(ELEMENT item, Node next) {
  18.             this.item = item;
  19.             this.next = next;
  20.         }
  21.  
  22.         @Override
  23.         public String toString() {
  24.             return this.item.toString();
  25.         }
  26.     }
  27.  
  28.     private Node head;
  29.     private int count;
  30.     private Node tail;
  31.  
  32.     public int size() {
  33.         return this.count;
  34.     }
  35.  
  36.     public SimpleLinkedListIterator() {
  37.         this.head = null;
  38.         this.count = 0;
  39.         this.tail = null;
  40.     }
  41.  
  42.     public void addFirst(ELEMENT item) {
  43.         Node temp = new Node(item, this.head);
  44.         if (this.count == 0) {
  45.             this.tail = temp;
  46.         }
  47.         this.head = temp;
  48.         ++this.count;
  49.     }
  50.  
  51.     public void addLast(ELEMENT item) {
  52.         Node temp = new Node(item, null);
  53.         if (this.count == 0) {
  54.             this.head = temp;
  55.         } else {
  56.             this.tail.next = temp;
  57.         }
  58.         this.tail = temp;
  59.         ++this.count;
  60.     }
  61.  
  62.     public ELEMENT removeFirst() {
  63.         if (this.count == 0) {
  64.             throw new RuntimeException("La lista está vacía...");
  65.         }
  66.         ELEMENT item = this.head.item;
  67.         this.head = this.head.next;
  68.         if (this.head == null) {
  69.             this.tail = null;
  70.         }
  71.         --this.count;
  72.         return item;
  73.     }
  74.  
  75.     public ELEMENT removeLast() {
  76.         if (this.count == 0) {
  77.             throw new RuntimeException("La lista está vacía...");
  78.         }
  79.         ELEMENT item = this.tail.item;
  80.         if (this.head.next == null) {
  81.             this.head = this.tail = null;
  82.         } else {
  83.             Node skip = this.head;
  84.             while (skip.next.next != null) {
  85.                 skip = skip.next;
  86.             }
  87.             this.tail = skip;
  88.             this.tail.next = null;
  89.         }
  90.         --this.count;
  91.         return item;
  92.     }
  93. /*
  94.     public void Mostrar() {
  95.         for (Node skip = this.head; skip != null; skip = skip.next) {
  96.             System.out.printf("%s ", skip.item.toString());
  97.         }
  98.         System.out.println();
  99.     }
  100. */
  101.  
  102.  
  103.  
  104.  
  105.     @Override
  106.     public Iterator<ELEMENT> iterator() {
  107.         return new MyIterator(this.head);
  108.     }
  109.  
  110.  
  111.     private class MyIterator implements Iterator<ELEMENT> {
  112.         private Node current;
  113.  
  114.         public MyIterator(Node current) {
  115.             this.current = current;
  116.         }
  117.  
  118.         @Override
  119.         public boolean hasNext() {
  120.             return this.current != null;
  121.         }
  122.  
  123.         @Override
  124.         public ELEMENT next() {
  125.             if (!this.hasNext()) {
  126.                 throw new RuntimeException("La lista está vacía...");
  127.             }
  128.             ELEMENT item = this.current.item;
  129.             this.current = this.current.next;
  130.             return item;
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement