Advertisement
jtentor

DemoList1 - SimpleLinkedList.java

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