Advertisement
jtentor

DemoList2 - DoubleLinkedList.java

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