Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ed_ficha02;
- /**
- *
- * @author franc
- */
- public class LinkedList<T> {
- Node head;
- Node tail;
- public LinkedList() {
- }
- public void add(T value) {
- Node newNode = new Node(value);
- if (this.tail == null) {
- this.tail = newNode;
- } else {
- this.head.setNext(newNode);
- }
- this.head = newNode;
- }
- public void remove(T value) {
- Node n = this.tail;
- if (value.equals(this.tail.getValue())) {
- this.tail = n.getNext();
- } else {
- Node previous = n;
- while (n != null) {
- if (n.getValue().equals(value)) {
- previous.setNext(n.getNext());
- break;
- }
- previous = n;
- n = n.getNext();
- }
- this.head = previous;
- }
- }
- public void printElements() {
- System.out.println("Lista ligada");
- Node n = this.tail;
- while (n != null) {
- System.out.println(n.value);
- n = n.getNext();
- }
- }
- }
- ///////////////// Class Node /////////////////
- public class Node<T> {
- T value;
- Node next;
- public Node() {
- }
- Node(T value) {
- this.value = value;
- }
- public void setNext(Node n){
- this.next = n;
- }
- public void setValue(T value){
- this.value = value;
- }
- public Node getNext(){
- return this.next;
- }
- public T getValue(){
- return this.value;
- }
- @Override
- public int hashCode() {
- int hash = 7;
- return hash;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final Node<?> other = (Node<?>) obj;
- if (!Objects.equals(this.value, other.value)) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement