Roke98

SimpleLinkedList

Oct 30th, 2022
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.61 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") class Node<ELEMENT> {
  12.    public ELEMENT item;
  13.    public Node<ELEMENT> next;
  14.  
  15.    @SuppressWarnings("unused")
  16.   public Node() {
  17.        this(null, null);
  18.    }
  19.    @SuppressWarnings("unused")
  20.   public Node(ELEMENT item) {
  21.        this(item, null);
  22.    }
  23.    public Node(ELEMENT item, Node<ELEMENT> next) {
  24.        this.item = item;
  25.        this.next = next;
  26.    }
  27.  
  28.    @Override
  29.    public String toString() {
  30.        return this.item.toString();
  31.    }
  32. }
  33. //endregion
  34.  
  35. //region Attributes
  36.  
  37. protected Node<ELEMENT> head;
  38. protected int count;
  39. protected Node<ELEMENT> tail;
  40. //endregion
  41.  
  42. //region Constructors
  43.  
  44. public SimpleLinkedList() {
  45.    this.head = null;
  46.    this.count = 0;
  47.    this.tail = null;
  48. }
  49. //endregion
  50.  
  51. //region Linked List Methods
  52.  
  53. // Returns the number of elements in this list.
  54. public int size() {
  55.    return this.count;
  56. }
  57.  
  58. public void addFirstRookieVersion(ELEMENT item) {
  59.    if (this.count == 0) {
  60.        this.head = this.tail = new Node<ELEMENT>(item, null);
  61.        ++this.count;
  62.    } else {
  63.        Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  64.        temp.next = this.head;
  65.        this.head = temp;
  66.        ++this.count;
  67.    }
  68. }
  69. // Inserts the specified element at the beginning of this list.
  70. public void addFirst(ELEMENT item) {
  71.    Node<ELEMENT> temp = new Node<ELEMENT>(item, this.head);
  72.    if (this.count == 0) {
  73.        this.tail = temp;
  74.    }
  75.    this.head = temp;
  76.    ++this.count;
  77. }
  78.  
  79. public void addLastRookieVersion(ELEMENT item) {
  80.    if (this.count == 0) {
  81.        this.head = this.tail = new Node<ELEMENT>(item, null);
  82.        ++this.count;
  83.    } else {
  84.        Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  85.        this.tail.next = temp;
  86.        this.tail = temp;
  87.        ++this.count;
  88.    }
  89. }
  90. // Appends the specified element to the end of this list.
  91. public void addLast(ELEMENT e) {
  92.    Node<ELEMENT> temp = new Node<ELEMENT>(e, null);
  93.    if (this.count == 0) {
  94.        this.head = temp;
  95.    } else {
  96.        this.tail.next = temp;
  97.    }
  98.    this.tail = temp;
  99.    ++this.count;
  100. }
  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 get(int i) {
  138.     rangeCheck(i);
  139.     Node list = head;
  140.     for (int k = 0; k < i; k++) {
  141.         list = list.next;
  142.     }
  143.     return (ELEMENT) list.item;
  144. }
  145.  
  146. public int indexOf(ELEMENT t) {
  147.     Node list = head;
  148.     int count = 0;
  149.     while (list != null) {
  150.         if (list.item.equals(t)) {
  151.             return count;
  152.         }
  153.         count++;
  154.         list = list.next;
  155.     }
  156.     return -1;
  157. }
  158.  
  159. private void rangeCheck(int i) {
  160.     if (i < 0 || i >= size()) {
  161.         throw new IndexOutOfBoundsException("index out of bounds " + i
  162.                 + " of " + size());
  163.     }
  164. }
  165.  
  166.   public ELEMENT element() {
  167.       if(this.size()<=0) {
  168.           System.out.println("La cola está vacía");
  169.       }
  170.      
  171.       return (ELEMENT) this.head.item;
  172.   }
  173.  
  174.   @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
  175.   public void addInOrder(ELEMENT item) {
  176.       if(this.count==0) {
  177.           this.head = this.tail = new Node(item,null);
  178.           ++this.count;
  179.       }else {
  180.           if(item.compareTo(this.head.item)<=0) {
  181.               this.addFirst(item);
  182.           }else {
  183.               if (item.compareTo(this.tail.item)>0) {
  184.                   this.addLast(item);
  185.               }else {
  186.                   Node skip = this.head;
  187.                   for(;(skip!=null)&(item.compareTo((ELEMENT) skip.item)>0);skip = skip.next) {}
  188.                   if (skip == null) {
  189.                       throw new RuntimeException("Algo está mal respecto del orden de los elementos");
  190.                   }else {
  191.                       Node temp = new Node(item,skip.next);
  192.                       skip.next = temp;
  193.                       ++this.count;
  194.                   }
  195.               }
  196.           }
  197.       }
  198.   }
  199.  
  200.   @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
  201.   public void addInOrderForVideoTitulo(Videos video) {
  202.       if(this.count==0) {
  203.           this.head = this.tail = new Node(video,null);
  204.           ++this.count;
  205.       }else {
  206.           if(video.compareTitulo(this.head.item)<=0) {
  207.               this.addFirst((ELEMENT) video);
  208.           }else {
  209.               if (video.compareTitulo(this.tail.item)>0) {
  210.                   this.addLast((ELEMENT) video);
  211.               }else {
  212.                   Node skip = this.head;
  213.                   for(;(skip!=null)&(video.compareTitulo((ELEMENT) skip.item)>0);skip = skip.next) {}
  214.                   if (skip == null) {
  215.                       throw new RuntimeException("Algo está mal respecto del orden de los elementos");
  216.                   }else {
  217.                       Node temp = new Node(video,skip.next);
  218.                       skip.next = temp;
  219.                       ++this.count;
  220.                   }
  221.               }
  222.           }
  223.       }
  224.   }
  225. //endregion
  226.  
  227.  
  228. //endregion
  229.  
  230. //region Object Methods
  231.  
  232. @Override
  233. public String toString() {
  234.  
  235.    if (this.size() <=0) {
  236.        return "";
  237.    }
  238.  
  239.    // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  240.    StringBuilder sb = new StringBuilder();
  241.  
  242.    sb.append("[" + this.head.item.toString());
  243.    for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  244.        sb.append(", " + skip.item.toString());
  245.    }
  246.    sb.append("]");
  247.  
  248.    return sb.toString();
  249. }
  250.  
  251. public Object[] toArray() {
  252.    Object[] result = new Object[this.size()];
  253.    int i = 0;
  254.    for(Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next, i++) {
  255.        result[i] = skip.item.toString();
  256.    }
  257.    return result;
  258. }
  259.  
  260. public boolean search(Object object) {
  261.     if (this.size()<=0)return false;
  262.     for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  263.         if (this.head.item.equals(object)) {
  264.             return true;
  265.         }
  266.     }
  267.     return false;
  268. }
  269. //endregion
  270.  
  271.  
  272. //region Iterable Methods
  273. @Override
  274. public Iterator<ELEMENT> iterator() {
  275.    return new SimpleLinkedListIterator(this.head);
  276. }
  277.  
  278. private class SimpleLinkedListIterator implements Iterator<ELEMENT> {
  279.    private Node<ELEMENT> current;
  280.  
  281.    public SimpleLinkedListIterator(Node<ELEMENT> current) {
  282.        this.current = current;
  283.    }
  284.  
  285.    @Override
  286.    public boolean hasNext() {
  287.        return this.current != null;
  288.    }
  289.  
  290.    @Override
  291.    public ELEMENT next() {
  292.        if (!this.hasNext()) {
  293.            throw new RuntimeException("La lista está vacía...");
  294.        }
  295.        ELEMENT item = this.current.item;
  296.        this.current = this.current.next;
  297.        return item;
  298.    }
  299.    
  300.  
  301. }
  302.  
  303.  
  304.  
  305. //endregion
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment