Advertisement
andresnogales

SimpleLinkedList.java

Oct 27th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. //Grupo 2.3
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class SimpleLinkedList<ELEMENT>  implements ILinkedList<ELEMENT>{
  6.    
  7.     private class Node<ELEMENT> {
  8.        
  9.         public ELEMENT item;
  10.        
  11.         public Node<ELEMENT> next;
  12.  
  13.         public Node() {
  14.             this(null, null);
  15.         }
  16.         public Node(ELEMENT item) {
  17.             this(item, null);
  18.         }
  19.         public Node(ELEMENT item, Node<ELEMENT> next) {
  20.             this.item = item;
  21.             this.next = next;
  22.         }
  23.  
  24.         @Override
  25.         public String toString() {
  26.             return this.item.toString();
  27.         }
  28.     }
  29.    
  30.     private class SimpleLinkedListIterator<ELEMENT> implements Iterator<ELEMENT>{
  31.         private Node<ELEMENT> current;
  32.        
  33.          public SimpleLinkedListIterator(Node<ELEMENT> current) {
  34.              this.current = current;
  35.          }
  36.          
  37.          @Override
  38.          public boolean hasNext() {
  39.              return this.current != null;
  40.          }
  41.  
  42.          @Override
  43.          public ELEMENT next() {
  44.              if (!this.hasNext()) {
  45.                  throw new RuntimeException("La lista está vacía");
  46.              }
  47.              ELEMENT item = this.current.item;
  48.              this.current = this.current.next;
  49.              return item;
  50.          }
  51.     }
  52.        
  53.     protected Node<ELEMENT> head;
  54.     protected Integer count;
  55.     protected Node<ELEMENT> tail;
  56.    
  57.     public SimpleLinkedList() {
  58.         head = null;
  59.         count = 0;
  60.         tail = null;
  61.     }
  62.    
  63.     public int size() {
  64.         return this.count;
  65.     }
  66.    
  67.     public void addFirstRookieVersion(ELEMENT item) {
  68.         if (this.count == 0) {
  69.             this.head = this.tail = new Node<ELEMENT>(item, null);
  70.             ++this.count;
  71.         } else {
  72.             Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  73.             temp.next = this.head;
  74.             this.head = temp;
  75.             ++this.count;
  76.         }
  77.     }
  78.    
  79.     public void addFirst(ELEMENT item) {
  80.         Node<ELEMENT> temp = new Node<ELEMENT>(item, this.head);
  81.         if (this.count == 0) {
  82.             this.tail = temp;
  83.         }
  84.         this.head = temp;
  85.         ++this.count;
  86.     }
  87.    
  88.     public void addLastRookieVersion(ELEMENT item) {
  89.         if (this.count == 0) {
  90.             this.head = this.tail = new Node<ELEMENT>(item, null);
  91.             ++this.count;
  92.         } else {
  93.             Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  94.             this.tail.next = temp;
  95.             this.tail = temp;
  96.             ++this.count;
  97.         }
  98.     }
  99.    
  100.     public void addLast(ELEMENT item) {
  101.         Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  102.         if (this.count == 0) {
  103.             this.head = temp;
  104.         } else {
  105.             this.tail.next = temp;
  106.         }
  107.         this.tail = temp;
  108.         ++this.count;
  109.     }
  110.    
  111.     public ELEMENT removeFirst() {
  112.         if (this.count == 0) {
  113.             throw new RuntimeException("La lista está vacía...");
  114.         }
  115.         ELEMENT item = this.head.item;
  116.         this.head = this.head.next;
  117.         if (this.head == null) {
  118.             this.tail = null;
  119.         }
  120.         --this.count;
  121.         return item;
  122.     }
  123.    
  124.     public ELEMENT removeLast() {
  125.         if (this.count == 0) {
  126.             throw new RuntimeException("La lista está vacía...");
  127.         }
  128.         ELEMENT item = this.tail.item;
  129.         if (this.head.next == null) {
  130.             this.head = this.tail = null;
  131.         } else {
  132.             Node<ELEMENT> skip = this.head;
  133.             while (skip.next.next != null) {
  134.                 skip = skip.next;
  135.             }
  136.             this.tail = skip;
  137.             this.tail.next = null;
  138.         }
  139.         --this.count;
  140.         return item;
  141.     }
  142.    
  143.     public ELEMENT getFirst() {    
  144.         return head.item;
  145.     }
  146.    
  147.     public ELEMENT getLast() {
  148.         return tail.item;
  149.     }
  150.        
  151.     @Override
  152.     public String toString() {
  153.  
  154.         if (this.size() <= 0) {
  155.             return "";
  156.         }
  157.         StringBuilder sb = new StringBuilder();
  158.  
  159.         sb.append("[" + this.head.item.toString());
  160.         for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  161.             sb.append(", " + skip.item.toString());
  162.         }
  163.         sb.append("]");
  164.  
  165.         return sb.toString();
  166.     }
  167.    
  168.     @Override
  169.     public Iterator<ELEMENT> iterator() {
  170.         return new SimpleLinkedListIterator(this.head);
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement