Advertisement
LEANDRONIEVA

SimpleLinkedList

Oct 25th, 2022 (edited)
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 KB | None | 0 0
  1. //
  2. //Created by Julio Tentor <[email protected]>
  3. //
  4.  
  5. import java.util.Iterator;
  6.  
  7. public class SimpleLinkedList<ELEMENT extends Comparable <ELEMENT>> implements ILinkedList<ELEMENT> {
  8.  
  9.  //region Node Class
  10.  
  11.  @SuppressWarnings("hiding")
  12. private class Node<ELEMENT> {
  13.      public ELEMENT item;
  14.      public Node<ELEMENT> next;
  15.  
  16.      @SuppressWarnings("unused")
  17.     public Node() {
  18.          this(null, null);
  19.      }
  20.      @SuppressWarnings("unused")
  21.     public Node(ELEMENT item) {
  22.          this(item, null);
  23.      }
  24.      public Node(ELEMENT item, Node<ELEMENT> next) {
  25.          this.item = item;
  26.          this.next = next;
  27.      }
  28.  
  29.      @Override
  30.      public String toString() {
  31.          return this.item.toString();
  32.      }
  33.  }
  34.  //endregion
  35.  
  36.  //region Attributes
  37.  
  38.  protected Node<ELEMENT> head;
  39.  protected int count;
  40.  protected Node<ELEMENT> tail;
  41.  //endregion
  42.  
  43.  //region Constructors
  44.  
  45.  public SimpleLinkedList() {
  46.      this.head = null;
  47.      this.count = 0;
  48.      this.tail = null;
  49.  }
  50.  //endregion
  51.  
  52.  //region Linked List Methods
  53.  
  54.  // Returns the number of elements in this list.
  55.  public int size() {
  56.      return this.count;
  57.  }
  58.  
  59.  public void addFirstRookieVersion(ELEMENT item) {
  60.      if (this.count == 0) {
  61.          this.head = this.tail = new Node<ELEMENT>(item, null);
  62.          ++this.count;
  63.      } else {
  64.          Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  65.          temp.next = this.head;
  66.          this.head = temp;
  67.          ++this.count;
  68.      }
  69.  }
  70.  // Inserts the specified element at the beginning of this list.
  71.  public void addFirst(ELEMENT item) {
  72.      Node<ELEMENT> temp = new Node<ELEMENT>(item, this.head);
  73.      if (this.count == 0) {
  74.          this.tail = temp;
  75.      }
  76.      this.head = temp;
  77.      ++this.count;
  78.  }
  79.  
  80.  public void addLastRookieVersion(ELEMENT item) {
  81.      if (this.count == 0) {
  82.          this.head = this.tail = new Node<ELEMENT>(item, null);
  83.          ++this.count;
  84.      } else {
  85.          Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  86.          this.tail.next = temp;
  87.          this.tail = temp;
  88.          ++this.count;
  89.      }
  90.  }
  91.  // Appends the specified element to the end of this list.
  92.  public void addLast(ELEMENT e) {
  93.      Node<ELEMENT> temp = new Node<ELEMENT>(e, null);
  94.      if (this.count == 0) {
  95.          this.head = temp;
  96.      } else {
  97.          this.tail.next = temp;
  98.      }
  99.      this.tail = temp;
  100.      ++this.count;
  101.  }
  102.  
  103.  // Removes and returns the first element from this list.
  104.  public ELEMENT removeFirst() {
  105.      if (this.count == 0) {
  106.          throw new RuntimeException("La lista está vacía...");
  107.      }
  108.      ELEMENT item = this.head.item;
  109.      this.head = this.head.next;
  110.      if (this.head == null) {
  111.          this.tail = null;
  112.      }
  113.      --this.count;
  114.      return item;
  115.  }
  116.  
  117.  // Removes and returns the last element from this list.
  118.  public ELEMENT removeLast() {
  119.      if (this.count == 0) {
  120.          throw new RuntimeException("La lista está vacía...");
  121.      }
  122.      ELEMENT item = this.tail.item;
  123.      if (this.head.next == null) {
  124.          this.head = this.tail = null;
  125.      } else {
  126.          Node<ELEMENT> skip = this.head;
  127.          while (skip.next.next != null) {
  128.              skip = skip.next;
  129.          }
  130.          this.tail = skip;
  131.          this.tail.next = null;
  132.      }
  133.      --this.count;
  134.      return item;
  135.  }
  136.  
  137.     public ELEMENT element() {
  138.         if(this.size()<=0) {
  139.             System.out.println("La cola está vacía");
  140.         }
  141.        
  142.         return (ELEMENT) this.head.item;
  143.     }
  144.    
  145.     @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
  146.     public void addInOrder(ELEMENT item) {
  147.         if(this.count==0) {
  148.             this.head = this.tail = new Node(item,null);
  149.             ++this.count;
  150.         }else {
  151.             if(item.compareTo(this.head.item)<=0) {
  152.                 this.addFirst(item);
  153.             }else {
  154.                 if (item.compareTo(this.tail.item)>0) {
  155.                     this.addLast(item);
  156.                 }else {
  157.                     Node skip = this.head;
  158.                     for(;(skip!=null)&(item.compareTo((ELEMENT) skip.item)>0);skip = skip.next) {}
  159.                     if (skip == null) {
  160.                         throw new RuntimeException("Algo está mal respecto del orden de los elementos");
  161.                     }else {
  162.                         Node temp = new Node(item,skip.next);
  163.                         skip.next = temp;
  164.                         ++this.count;
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.     }
  170.    
  171.     @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
  172.     public void addInOrderForVideoTitulo(Video video) {
  173.         if(this.count==0) {
  174.             this.head = this.tail = new Node(video,null);
  175.             ++this.count;
  176.         }else {
  177.             if(video.compareTitulo(this.head.item)<=0) {
  178.                 this.addFirst((ELEMENT) video);
  179.             }else {
  180.                 if (video.compareTitulo(this.tail.item)>0) {
  181.                     this.addLast((ELEMENT) video);
  182.                 }else {
  183.                     Node skip = this.head;
  184.                     for(;(skip!=null)&(video.compareTitulo((ELEMENT) skip.item)>0);skip = skip.next) {}
  185.                     if (skip == null) {
  186.                         throw new RuntimeException("Algo está mal respecto del orden de los elementos");
  187.                     }else {
  188.                         Node temp = new Node(video,skip.next);
  189.                         skip.next = temp;
  190.                         ++this.count;
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.     }
  196.  //endregion
  197.  
  198.  //region Object Methods
  199.  
  200.  @Override
  201.  public String toString() {
  202.  
  203.      if (this.size() <=0) {
  204.          return "";
  205.      }
  206.  
  207.      // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  208.      StringBuilder sb = new StringBuilder();
  209.  
  210.      sb.append("[" + this.head.item.toString());
  211.      for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  212.          sb.append(", \n" + skip.item.toString());
  213.      }
  214.      sb.append("]");
  215.  
  216.      return sb.toString();
  217.  }
  218.  
  219.  public Object[] toArray() {
  220.      Object[] result = new Object[this.size()];
  221.      int i = 0;
  222.      for(Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next, i++) {
  223.          result[i] = skip.item.toString();
  224.      }
  225.      return result;
  226.  }
  227.  
  228.  public boolean search(Object object) {
  229.      if (this.size()<=0)return false;
  230.      for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  231.          if (this.head.item.equals(object)) {
  232.              return true;
  233.          }
  234.      }
  235.      return false;
  236.  }
  237.  //endregion
  238.  
  239.  
  240.  //region Iterable Methods
  241.  @Override
  242.  public Iterator<ELEMENT> iterator() {
  243.      return new SimpleLinkedListIterator(this.head);
  244.  }
  245.  
  246.  private class SimpleLinkedListIterator implements Iterator<ELEMENT> {
  247.      private Node<ELEMENT> current;
  248.  
  249.      public SimpleLinkedListIterator(Node<ELEMENT> current) {
  250.          this.current = current;
  251.      }
  252.  
  253.      @Override
  254.      public boolean hasNext() {
  255.          return this.current != null;
  256.      }
  257.  
  258.      @Override
  259.      public ELEMENT next() {
  260.          if (!this.hasNext()) {
  261.              throw new RuntimeException("La lista está vacía...");
  262.          }
  263.          ELEMENT item = this.current.item;
  264.          this.current = this.current.next;
  265.          return item;
  266.      }
  267.  
  268.  }
  269.  
  270.  
  271.  //endregion
  272.  
  273. }
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement