Advertisement
Jurado001

TP4ej2_List

Oct 12th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. public class List<E> {
  2.     private Node head;
  3.     private int count;
  4.     private Node tail;
  5.  
  6.    
  7.     public boolean isEmpty() {
  8.         return count <= 0;
  9.     }
  10.    
  11.     public int getCount() {
  12.         return this.count;
  13.     }
  14.    
  15.     //Metodos
  16.     public List() {
  17.         this.head = null;
  18.         this.count = 0;
  19.         this.tail = null;
  20.     }
  21.    
  22.    
  23.     public void AddToHead(E item) {
  24.         Node temp = new Node(item, this.head);
  25.         if(this.count == 0) {
  26.             this.tail = temp;
  27.         }
  28.         this.head = temp;
  29.         ++this.count;
  30.     }
  31.    
  32.     public void AddToTail(E item) {
  33.         Node temp = new Node(item, null);
  34.         if(this.count == 0) {
  35.             this.head = temp;
  36.         }
  37.         else {
  38.             this.tail.next = temp;
  39.         }
  40.         this.tail = temp;
  41.         ++this.count;
  42.     }
  43.    
  44.    
  45.    
  46.     public E RemoveFromHead() {
  47.         if(this.count == 0) {
  48.             throw new RuntimeException("La lista está vacia...");
  49.         }
  50.         E item = this.head.item;
  51.         this.head = this.head.next;
  52.         if(this.head == null) {
  53.             this.tail = null;
  54.         }
  55.         --this.count;
  56.         return item;
  57.     }
  58.    
  59.     public E RemoveFromTail() {
  60.         if(this.count == 0) {
  61.             throw new RuntimeException("La lista está vacia...");
  62.         }
  63.         E item = this.tail.item;
  64.         if(this.head.next == null) {
  65.             this.head = this.tail = null;
  66.         }
  67.         else {
  68.             Node skip = this.head;
  69.             for( ; skip.next.next != null; skip = skip.next) { }
  70.             this.tail = skip;
  71.             this.tail.next = null;
  72.         }
  73.         --this.count;
  74.         return item;
  75.     }
  76.    
  77.     public void Mostrar() {
  78.         for(Node skip = this.head; skip != null; skip = skip.next) {
  79.             System.out.println(skip);
  80.         }
  81.     }
  82.    
  83.    
  84.     private class Node {
  85.         public E item;
  86.         public Node next;
  87.        
  88.         //Getters & Setters    
  89.         public E getItem() {
  90.             return item;
  91.         }
  92.  
  93.         public void setItem(E item) {
  94.             this.item = item;
  95.         }
  96.  
  97.         public Node getNext() {
  98.             return next;
  99.         }
  100.  
  101.         public void setNext(Node next) {
  102.             this.next = next;
  103.         }
  104.  
  105.         //Metodos
  106.         public Node() {
  107.             this(null, null);
  108.         }
  109.        
  110.         public Node(E item) {
  111.             this(item, null);
  112.         }
  113.        
  114.         public Node(E item, Node next) {
  115.             this.item = item;
  116.             this.next = next;
  117.         }
  118.        
  119.         public Node(Node next) {
  120.             this(null,next);
  121.         }
  122.        
  123.  
  124.         public String toString() {
  125.             return this.item.toString();
  126.         }
  127.        
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement