Advertisement
gabuwu

TP04-EJ4- SLL new

Nov 18th, 2021
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package TP4;
  2.  
  3.  
  4. import java.util.Iterator;
  5.  
  6. public class SimpleLinkedList<ELEMENT> implements ILinkedList<ELEMENT> {
  7.  
  8.     //region Node Class
  9.  
  10.     private class Node<ELEMENT> {
  11.         public ELEMENT item;
  12.         public Node<ELEMENT> next;
  13.  
  14.         public Node() {
  15.             this(null, null);
  16.         }
  17.         public Node(ELEMENT item) {
  18.             this(item, null);
  19.         }
  20.         public Node(ELEMENT item, Node<ELEMENT> next) {
  21.             this.item = item;
  22.             this.next = next;
  23.         }
  24.  
  25.         @Override
  26.         public String toString() {
  27.             return this.item.toString();
  28.         }
  29.     }
  30.     //endregion
  31.  
  32.     //region Attributes
  33.  
  34.     protected Node<ELEMENT> head;
  35.     protected int count;
  36.     protected Node<ELEMENT> tail;
  37.     //endregion
  38.  
  39.     //region Constructors
  40.  
  41.     public SimpleLinkedList() {
  42.         this.head = null;
  43.         this.count = 0;
  44.         this.tail = null;
  45.     }
  46.     //endregion
  47.  
  48.     //region Linked List Methods
  49.  
  50.     // Returns the number of elements in this list.
  51.     public int size() {
  52.         return this.count;
  53.     }
  54.  
  55.    
  56.    
  57.  
  58.     // Appends the specified element to the end of this list.
  59.     public void addLast(ELEMENT item) {
  60.         Node<ELEMENT> temp = new Node<ELEMENT>(item, null);
  61.         if (this.count == 0) {
  62.             this.head = temp;
  63.         } else {
  64.             this.tail.next = temp;
  65.         }
  66.         this.tail = temp;
  67.         ++this.count;
  68.     }
  69.  
  70.     public ELEMENT removeLast() {
  71.         if (this.count == 0) {
  72.             throw new RuntimeException("La lista está vacía...");
  73.         }
  74.         ELEMENT item = this.tail.item;
  75.         if (this.head.next == null) {
  76.             this.head = this.tail = null;
  77.         } else {
  78.             Node<ELEMENT> skip = this.head;
  79.             while (skip.next.next != null) {
  80.                 skip = skip.next;
  81.             }
  82.             this.tail = skip;
  83.             this.tail.next = null;
  84.         }
  85.         --this.count;
  86.         return item;
  87.     }
  88.  
  89.     //region Object Methods
  90.  
  91.     @Override
  92.     public String toString() {
  93.  
  94.         if (this.size() <=0) {
  95.             return "";
  96.         }
  97.  
  98.         // from https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/StringBuilder.html
  99.         StringBuilder sb = new StringBuilder();
  100.  
  101.         sb.append("[" + this.head.item.toString());
  102.         for (Node<ELEMENT> skip = this.head.next; skip != null; skip = skip.next) {
  103.             sb.append("" + skip.item.toString());
  104.         }
  105.         sb.append("]");
  106.  
  107.         return sb.toString();
  108.     }
  109.     //endregion
  110.  
  111.  
  112.     //region Iterable Methods
  113.     @Override
  114.     public Iterator<ELEMENT> iterator() {
  115.         return new SimpleLinkedListIterator(this.head);
  116.     }
  117.  
  118.     private class SimpleLinkedListIterator implements Iterator<ELEMENT> {
  119.         private Node<ELEMENT> current;
  120.  
  121.         public SimpleLinkedListIterator(Node<ELEMENT> current) {
  122.             this.current = current;
  123.         }
  124.  
  125.         @Override
  126.         public boolean hasNext() {
  127.             return this.current != null;
  128.         }
  129.  
  130.         @Override
  131.         public ELEMENT next() {
  132.             if (!this.hasNext()) {
  133.                 throw new RuntimeException("La lista está vacía...");
  134.             }
  135.             ELEMENT item = this.current.item;
  136.             this.current = this.current.next;
  137.             return item;
  138.         }
  139.  
  140.     }
  141.  
  142.  
  143.     //endregion
  144.  
  145. }
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement