Advertisement
xickoh

ED Ficha 2 Exer 1

Oct 17th, 2020
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ed_ficha02;
  7.  
  8. /**
  9.  *
  10.  * @author franc
  11.  */
  12. public class LinkedList<T> {
  13.  
  14.     Node head;
  15.     Node tail;
  16.  
  17.     public LinkedList() {
  18.  
  19.     }
  20.  
  21.     public void add(T value) {
  22.         Node newNode = new Node(value);
  23.  
  24.         if (this.tail == null) {
  25.             this.tail = newNode;
  26.         } else {
  27.             this.head.setNext(newNode);
  28.         }
  29.         this.head = newNode;
  30.     }
  31.  
  32.     public void remove(T value) {
  33.         Node n = this.tail;
  34.  
  35.         if (value.equals(this.tail.getValue())) {
  36.             this.tail = n.getNext();
  37.         } else {
  38.             Node previous = n;
  39.             while (n != null) {
  40.                 if (n.getValue().equals(value)) {
  41.                     previous.setNext(n.getNext());
  42.                     break;
  43.                 }
  44.                 previous = n;
  45.                 n = n.getNext();
  46.             }
  47.             this.head = previous;
  48.         }
  49.     }
  50.  
  51.     public void printElements() {
  52.         System.out.println("Lista ligada");
  53.         Node n = this.tail;
  54.         while (n != null) {
  55.             System.out.println(n.value);
  56.             n = n.getNext();
  57.         }
  58.     }
  59.  
  60. }
  61.  
  62. ///////////////// Class Node /////////////////
  63.  
  64. public class Node<T> {
  65.     T value;
  66.     Node next;
  67.  
  68.     public Node() {
  69.     }
  70.    
  71.     Node(T value) {
  72.         this.value = value;
  73.     }
  74.    
  75.     public void setNext(Node n){
  76.         this.next = n;
  77.     }
  78.    
  79.     public void setValue(T value){
  80.         this.value = value;
  81.     }
  82.    
  83.     public Node getNext(){
  84.         return this.next;
  85.     }
  86.    
  87.     public T getValue(){
  88.         return this.value;
  89.     }
  90.  
  91.     @Override
  92.     public int hashCode() {
  93.         int hash = 7;
  94.         return hash;
  95.     }
  96.  
  97.     @Override
  98.     public boolean equals(Object obj) {
  99.         if (this == obj) {
  100.             return true;
  101.         }
  102.         if (obj == null) {
  103.             return false;
  104.         }
  105.         if (getClass() != obj.getClass()) {
  106.             return false;
  107.         }
  108.         final Node<?> other = (Node<?>) obj;
  109.         if (!Objects.equals(this.value, other.value)) {
  110.             return false;
  111.         }
  112.         return true;
  113.     }
  114. }
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement